sed variable and backslash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed variable and backslash
# 1  
Old 02-19-2010
sed variable and backslash

I am trying to write a bash shell script, which extracts a sentence from a dynamically created dynamic file and passes it as a variable to sed and uses that sentence as a starting point to pull the content from a log file.

the key part of the script is this

Code:
key=`cat /tmp/dynamic`

sed -n "/$key/,\$p" /logfile

the problem is, when the sentence has backslash in it, it totally screws the sed command, for example, if the sentence is "02/16/2010 06:43:33 PM CST : CRITICAL ERROR", when it gets passwd to sed, sed will see the backslash and that will cause problem,

Code:
sed -n "/02/16/2010 06:43:33 PM CST : CRITICAL ERROR/,\$p'

Anyone knows how can i pass backslash to sed as a variable in this case? thanks

Last edited by zxmaus; 02-19-2010 at 07:48 PM.. Reason: added code tags
# 2  
Old 02-19-2010
I see only normal slashes in your example,
If you want to escape backslash then use another backslash in front of it,
Or If you have many slash in the patterns like in your example then use the below form,then as i know you dont need to escape slashes:

Code:
sed -n '# # #' logfile

ok, let me give a good example:

assume you want to change the directory /usr/bin/sh with /usr/bin/bash then use this

Code:
sed 's#/usr/bin/sh#/usr/bin/bash#g'  file

otherwise you have to use many backslashes to escape slashes:

Code:
sed 's/\/usr\/bin\/sh/\/usr\/bin\/bash/g' file

# 3  
Old 02-19-2010
sorry i mean slash, not backslash,

i know i can use blackslash to escape slash, but the thing is i have to pass the variable to sed. and i cannot predict if the variable will have slash and where the slash will show up.
# 4  
Old 02-19-2010
Be aware that your approach, expanding a variable into a sed regular expression, can cause problems with other special characters, not just the slash. If it's a special regular expression character (., *, etc) and it appears in the variable's value, the script will not work correctly.

If forward slash is the only thing you're concerned about, Eagle's suggestion of using a different delimiter is the simplest solution (of course, if the chosen delimiter appears in the variable's value, you're back to square one). If other special characters may occur, perhaps AWK and non-regular expression substring operations would be wiser.

Code:
awk -v k="$key" 'index($0,k){i=1}i'


Last edited by alister; 02-20-2010 at 04:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add backslash and apostrophe to string in variable.

Hi All I want to add backslash and apostrophe to variable in my bash script. I have my variable: USER_LIST=USER1,USER2,USER3 and I want something like this: USER_LIST_DEL=/'USER1/',/'USER2/',/'USER3/' any ideas ?? (1 Reply)
Discussion started by: primo102
1 Replies

2. Shell Programming and Scripting

Adding a backslash in front of square brackets with sed

I'm trying to convert this line: to \ with sed. This is what I have so far: sed -e 's/\]*\)\]/\\\\\/' but this still gives me . Any suggestions? (15 Replies)
Discussion started by: lehaste
15 Replies

3. Shell Programming and Scripting

Passing backslash character to awk variable

Hi All. I have a file that contains some special characters and I'm trying to use AWK to search for lines between <pattern1> and <pattern2>. As an example: I need the lines between the line containing ' select_id="x_0 ' and the line containing the next instance of ' from '. This is a file... (5 Replies)
Discussion started by: Mudshark
5 Replies

4. UNIX for Dummies Questions & Answers

Replace backslash at the end of the string using sed command

I have text file which is a tab delimited one. Sample data from the file is shown below: unix is\ great\ os linux\ is superb I want to replace that backslash with empty string preserving the tab delimiter. Output should be unix is great os linux is ... (3 Replies)
Discussion started by: p.akhilreddy4u
3 Replies

5. Shell Programming and Scripting

How can I get sed to include backslash and 'f' in the output

Both of these fail. One has two form feeds, the second form leaves all the backslashes. bold='\(code\|command\|var\|samp\|option\|strong\)' sed -e "s;@${bold}{"'\(*\)};\fB\2\fP;g' sed -e "s;@${bold}{"'\(*\)};\\fB\2\\fP;g' Obviously, I'm trying to change texi markup into man page markup, but it... (3 Replies)
Discussion started by: bkorb
3 Replies

6. Shell Programming and Scripting

Cut on last backslash on hyperlink string-sed/awk??

hyper link- abc:8081/xyz/2.5.6/rtyp-2.5.6.jar Needs to get "rtyp-2.5.6.jar" i.e character after last backslash "/" how to do this using sed/awk?? help is highly appreciated. (7 Replies)
Discussion started by: kkscm
7 Replies

7. Shell Programming and Scripting

SED script to backslash special characters

I have a shell script that I have written to be a kind of to-do/notepad that's quickly executable from the command line. However, special characters tend to break it pretty well. Ie: "notes -a This is an entry." works fine. "notes -a This is (my) entry." will toss back a bash syntax error on... (5 Replies)
Discussion started by: skylersee
5 Replies

8. Shell Programming and Scripting

How to use backslash and variables in sed

I have a line that contains backslashes in which I want sed to substitute text with variables. The line; \\s008\2033330user$ I want to change this in \\s008.ourschool.com\2033330user$ I now use this script: USER=2033330user sed 's/\\'"$USER"'/.ourschool.com\\'"$USER/" This doesn't... (3 Replies)
Discussion started by: Tubbie
3 Replies

9. Shell Programming and Scripting

Non-inserting backslash in sed statement

#!/bin/bash wget -O tmp.tmp "YouTube - Pretty Woman- Vivian's Goes Shopping!" temp=`grep 'one&video_id=' tmp.tmp | sed "s/.*one&video_id=\(.*\)'\;.*/\1/"` temp="http://www.youtube.com/get_video?video_id=$temp" temp=`echo $temp|sed -n "s/!/\\!/p"` echo " -O $filename \"$temp\"" Output:... (3 Replies)
Discussion started by: kds1398
3 Replies

10. Shell Programming and Scripting

Sed and awk backslash characters

Hi, I have a variable read from user input: PROFILESROOTDIR="\\194.185.82.188\CMSRepository\EncodingProfiles" awk -F"=" -v gr=$PROFILESROOTDIR '/ProfilesRootDirectoryFromXOEMachine/{$2=gr;}1' OFS="=" $CFGFILE > "${CFGFILE}_new" For this awk to work properly I need to replace in the... (7 Replies)
Discussion started by: potro
7 Replies
Login or Register to Ask a Question