assigning a multiline grep output which has been piped through sed to a shell variabl

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

Maybe it's easier for us if you explain what you're trying to achieve and add some examples of the inputfile and the desired output within code tags (select the text with the mouse and click on the # above the edit box).

Regards

Hi,

I did include a sample input file along wit desired output.

I can't use the html editor for posting messages, as I'm blind, I'm using a screen reader, and the html editor is not accessible to the screen reader.

Can you not read my entire message?

Tim

Try this:

OUT=`awk '/test/{print $0 "\\\"}' /home/tim/input.txt`

echo "$OUT"

Regards

Hi,

This works in isolation, but if I then try and use $OUT as a replacement pattern in a sed command I get the sed 'command garbled' error.

i.e. my script has:

OUT=`awk '/test/{print $0 "\\\"}' /home/tim/input.txt`
sed "s/replaceme/$OUT/" $INPUTFILE > $OUTPUTFILE

The command garbled error message does expand the replacement pattern, so $OUT is recognized for what it is, but it doesn't like something in the format of the pattern.

Any ideas?

Thanks,

Tim

Do you want to replace one word with some lines with backslashes? I can't follow it, can you post some lines from your inputfile with words you want to replace and the desired output?

Regards

Hi,

Ok, what I have is an installation script for an application. If this script detects an existing installation of the application it attempts an upgrade. This involves:

  1. backing up the existing installation directory.
  2. installing a fresh installation.
  3. extracting specific lines from a configuration file in the backuped directory and inserting them, at a specific point, in the freshly installed config file.

So, I need to search for the line or lines in the old config file. This may return several lines of config. These lines must then be inserted in the new config file at a specific text marker, such as something like:

#@@Merge_config_here@@#

so, in my script I need to hold the extracted text in a variable and insert it in the new config file. The marker text also needs to be removed, so actually it is a replace.

Now, from what I've read, sed does not allow literal newline characters in a replacement pattern. If they exist then they must be escaped with a backslash. This is why I want to add a backslash to the end of each line I extract from the old config file.

However, whatever I'm doing, sed still doesn't like what it is getting.

I hope this clarifies things?

Tim

Ok, if I understand the question, you want to replace the specific marker with all the lines in the variable right?
Store the lines in a file instead of a variable without a backslash, for example:

awk '/test/' /home/tim/input.txt > insertfile

Insert the file at the specific marker and delete the marker with sed:

sed -e '/#@@Merge_config_here@@#/ r insertfile' -e '/#@@Merge_config_here@@#/d' config.file

Regards

Hi,

I wanted to avoid creating temperary files. There would be quite a few multiline settings to read in and merge into the new config. Therefore I would end up with lots of temperary files to create and later clean up.

Is it really not possible to do this internally in the script itself?

Why does the sed documentation say that escaping a newline with backslash will allow multiline replacement patterns when it doesn't seem to do so?

Thanks,

Tim