Hi,
I wish to format the output of a grep command in such a way that sed will be able to handle the newline characters held in the output.
Since sed does not allow newlines to be contained in a replacement pattern, that means adding a backslash '\' character to the end of each line from grep.
Once this is done I want the correctly formatted string to be held in a shell variable so I can submit it to another sed command.
For example my input file is:
test line1
test line2
test line3
The result held in a shell variable like $OUT would look like:
test line1\
test line2\
test line3\
The text held in $OUT would then be used as a replacement pattern in a subsequent sed command.
However, regardless of what commands I use, I always get a 'command garbled' error from sed.
What I've tried:
From the command prompt the below works, but it is not storing the result in a shell variable:
msgspb01.es.cpth.ie:/home/tim>grep test /home/tim/input.txt | sed 's/$/\\/'
test line1\
test line2\
testline3\
msgspb01.es.cpth.ie:/home/tim>
Now, if I try and store the result in $OUT:
msgspb01.es.cpth.ie:/home/tim>OUT=`grep test /home/tim/input.txt | sed 's/$/\\/'`
sed: command garbled: s/$/\/
msgspb01.es.cpth.ie:/home/tim>
I get the same result if I enclose the sed command in double quotes:
msgspb01.es.cpth.ie:/home/tim>OUT=`grep test /home/tim/input.txt | sed "s/$/\\/"`
sed: command garbled: s/$/\/
msgspb01.es.cpth.ie:/home/tim>
Note, I get the same result if I place the above commands in a /bin/sh script.
Does anybody know if it is possible to store the output of grep output filtered through sed and store it in a variable?
If not, How can I easily solve this problem?
system details:
output of uname -a:
SunOS msgspb01.es.cpth.ie 5.8 Generic_117350-51 sun4u sparc SUNW,Sun-Fire-280R
Though problem also occurs on linux.
I'm using the standard /usr/bin/sed installed on the system.
Many thanks,
Tim