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
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,
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
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:
ok, let me give a good example:
assume you want to change the directory /usr/bin/sh with /usr/bin/bash then use this
otherwise you have to use many backslashes to escape slashes:
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)