Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-19-2014, 12:39 AM   #1
LQ Newbie
Registered: Jan 2011
Posts: 5

Rep: Reputation: 0
Question Command to replace / with \ / in shell script


I wanted to print lines between a pattern (Time pattern - 2014/05/16 00:15:40.222) till end of file.

I googled, And was able to find the answer and used the below awk command,

Time='2014/05/16 00:15:40.222'
awk "/$Time/,0" $FileName

Still I was facing a problem, then added front slash \ before back slash in Time Variable. And works perfectly.

Time='2014\/05\/16 00:15:40.222'
awk "/$Time/,0" $FileName

But now my problem is, The Time variable is dynamic, i get it like this,

currentLastLine=`sed -n '$p' $FileName`
Time=`echo $currentLastLine | awk -F" " '{print $1" " $2}'`

Now I do not know how to add front slash to this Time variable.

Please help me in resolving this.
Old 05-19-2014, 03:07 AM   #2
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
In bash:
1 members found this post helpful.
Old 05-19-2014, 03:17 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
berbae's solution is fine, but I would probably just suggest doing it when assigned, so if you show us what data is stored in 'currentLastLine' we may be able to help further.

Also, please use [code][/code] tags when showing code or data to make it more legible and stand out from the rest of your text.
1 members found this post helpful.
Old 05-19-2014, 04:32 AM   #4
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36

To add a backslash before each slash:

sed 's?/?\\/?g'
Old 05-19-2014, 07:44 AM   #5
LQ Newbie
Registered: Jan 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you. It worked with berbae's solution.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
String replace in shell script jnreddy Programming 3 05-28-2011 02:07 AM
Find and replace with a shell script abefroman Programming 5 03-10-2009 03:38 PM
how to replace value of to using shell script amit_pansuria Programming 3 08-06-2008 08:30 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration