The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exclude Certain Entries from Piped or Redirected Output? downplay Shell Programming and Scripting 1 02-28-2009 04:58 PM
Problem with assigning output of grep + awk to a variable meheretoknow Shell Programming and Scripting 5 12-16-2008 04:50 AM
Assigning output of command to a variable in shell sankar reddy Shell Programming and Scripting 6 02-28-2008 03:01 AM
assigning nawk output to shell variable user_prady Shell Programming and Scripting 6 11-29-2007 04:01 AM
assigning command output to a shell variable kprattip Shell Programming and Scripting 2 07-09-2007 05:01 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 04-01-2009
timculhane timculhane is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 5
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0