LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

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


Hi,

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, 04:07 AM   #2
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
In bash:
Time="${Time//\//\\/}"
 
1 members found this post helpful.
Old 05-19-2014, 04:17 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 05:32 AM   #4
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36

To add a backslash before each slash:

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

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


Reply


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 10:12 PM
String replace in shell script jnreddy Programming 3 05-28-2011 03:07 AM
Find and replace with a shell script abefroman Programming 5 03-10-2009 04:38 PM
how to replace value of 4.1.0.1.22 to 4.1.0.0.22 using shell script amit_pansuria Programming 3 08-06-2008 09:30 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM


All times are GMT -5. The time now is 12:47 AM.

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