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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assigning a multiline grep output which has been piped through sed to a shell variabl
# 1  
Old 04-01-2009
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
# 2  
Old 04-01-2009
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
# 3  
Old 04-01-2009
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
# 4  
Old 04-01-2009
Try this:

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

echo "$OUT"

Regards
# 5  
Old 04-01-2009
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
# 6  
Old 04-02-2009
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
# 7  
Old 04-02-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cat piped output

Hello, How can I efficiently cat piped output with another file? > (awk command) | cat file1 (piped output) Thanks! (11 Replies)
Discussion started by: palex
11 Replies

2. Shell Programming and Scripting

script output should be piped to a file

hi i have a script named mount.sh under the location /data/scripts/ in my aix box i want this script this to be run everyday morning at 04:45 AM and the output of the script should be piped to a file how to do this ? (3 Replies)
Discussion started by: samsungsamsung
3 Replies

3. Shell Programming and Scripting

find output seems to change when piped

Currently, i am trying to create a simple robust script that is intended to move the contents of a given source directory to a target directory. Optionally, the script should allow to either move the whole source dir content, or dotfiles only, or visible files only. I am aware the target directory... (0 Replies)
Discussion started by: shells_bells
0 Replies

4. Shell Programming and Scripting

Concatenate piped output and some var?

Hello, There is pipe chain and I want concacenate piped data with some variable: balh blah| ... $var1 What command I should use instead ... to concatenate piped output with $var1. I think I coud solve this using temp var - but could it be done in one line like sample above ? thanks... (4 Replies)
Discussion started by: vilius
4 Replies

5. Shell Programming and Scripting

Assigning value of SQL output to a variable in shell scripting

I am trying to assgn the output of the select statement to a variable, like this "VARIABLE_NAME=$ db2 "select COLUMN_NAME_1 from TABLE_NAME where COLUMN_NAME_2='VALUE_TO_CHECK'"; " but the value that is getting into VARIABLE_NAME is "COLUMN_NAME_1 ----------------- VALUE 1... (3 Replies)
Discussion started by: sgmini
3 Replies

6. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

7. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

8. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

9. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

10. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies
Login or Register to Ask a Question