Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
parameters in sed
Name: kris92 Date: May 25, 2005 at 23:54:43 Pacific OS: UNIX CPU/Ram: XEON
Comment:
Hi I am using sed in a shell script. I am having problems in using parameters in sed for search and replace.
TEST=`grep "'/" source | head -1` sed s/$TEST/string1/g source.txt > target.txt This doesnt work but when i define the variable TEST as TEST="string2" sed s/$TEST/string1/g source.txt > target.txt
Name: Jim Boothe Date: May 26, 2005 at 05:46:11 Pacific
Reply:
Two things are messing up sed. After $TEST is evaluated, the single quote(s) that it brings in is now treated as a shell delimiter. To get around that, place your sed command within double-quotes. Secondly, $TEST is also bringing in a slash, and that messes up the sed command delineation. To get around that, switch the sed delimiter character to something other than slash that you know will not be in your $TEST or string1 fields.
With $TEST containing any number of single quotes and slashes, the following works for me:
sed "s!$TEST!string1!g"
0
Response Number 2
Name: kris92 Date: May 27, 2005 at 00:19:43 Pacific
Summary: Hi, Anyone know a way to use parameters in a sed or a way round it?? I'm trying to do something like this: sed 's/${USER}/${NEW_USER}/g' but it does nothing at all... i'm running this in the korn shel...
Summary: You are attempting to implement a replacement in sed. Arnold Robbins implemented a sed version in his "Sed & Awk" book published by O'Reilly. He called the command 'gres' - global regular expression ...
Summary: Hi Folks, In sed and/or awk, how would I formulate an if/else logic to skip a command if a specific text already exist in a file? I am getting multiple entries in a text file when the script is execu...