Appending sed output to variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Appending sed output to variable
# 1  
Old 10-24-2010
Appending sed output to variable

I want to append matched output and cat the results into an variable. but I've been running into problems. sed is printing result on to screen instead of appending the output to $CAPTURE. I'm stumped...how should i fix this?

Code:
contents of $TEST

10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4

Code:
expected data in $CAPTURE:

10.0.0.1
10.0.0.2
10.0.0.3

Code:
#!/usr/xpg4/bin/sh
export PATH="/usr/xpg4/bin:$PATH"

echo "$TEST" | sed -n 's/^10.0.0.1$/&/p' > $CAPTURE
echo "$TEST" | sed -n 's/^10.0.0.2$/&/p' > $CAPTURE
echo "$TEST" | sed -n 's/^10.0.0.3$/&/p' > $CAPTURE

# 2  
Old 10-24-2010
Code:
CAPTURE=$(echo "$TEST" | sed -n 's/^10.0.0.1$/&/p'
echo "$TEST" | sed -n 's/^10.0.0.2$/&/p'
echo "$TEST" | sed -n 's/^10.0.0.3$/&/p')

# 3  
Old 10-24-2010
Quote:
Originally Posted by jlliagre
Code:
CAPTURE=$(echo "$TEST" | sed -n 's/^10.0.0.1$/&/p'
echo "$TEST" | sed -n 's/^10.0.0.2$/&/p'
echo "$TEST" | sed -n 's/^10.0.0.3$/&/p')

thanks, but now I've another problem when:

contents in
$TEST
10.0.0.1
10.0.0.2
10.0.0.21
10.0.0.3
10.0.0.4

contents in
$CAPTURE
10.0.0.1
10.0.0.2
10.0.0.21
10.0.0.21 <-- duplicated entry? what happened?
10.0.0.3

Code:
#!/usr/xpg4/bin/sh
export PATH="/usr/xpg4/bin:$PATH"

TEST=$(echo 10.0.0.1
        echo 10.0.0.2
        echo 10.0.0.21
        echo 10.0.0.3
        echo 10.0.0.4
        )

CAPTURE=$(echo "$TEST" | sed -n 's/^10.0.0.1/&/p'
echo "$TEST" | sed -n 's/^10.0.0.2/&/p'
echo "$TEST" | sed -n 's/^10.0.0.21/&/p'
echo "$TEST" | sed -n 's/^10.0.0.3/&/p')

printf "$CAPTURE"


Last edited by jazzaddict; 10-24-2010 at 01:52 AM..
# 4  
Old 10-24-2010
you can maybe try delete others Smilie
Code:
# TEST=$(echo 10.0.0.1
        echo 10.0.0.2
        echo 10.0.0.21
        echo 10.0.0.3
        echo 10.0.0.4
        )

Code:
# echo "$TEST"| sed '/10.0.0.[123]$/!d'
10.0.0.1
10.0.0.2
10.0.0.3

Code:
# echo "$TEST"| sed '/10.0.0.[4]$/d'
10.0.0.1
10.0.0.2
10.0.0.21
10.0.0.3

if you want to your method
then try correct in below in your script (add $ char so means end of line )

Code:
TEST=$(echo 10.0.0.1
        echo 10.0.0.2
        echo 10.0.0.21
        echo 10.0.0.3
        echo 10.0.0.4
        )

CAPTURE=$(echo "$TEST" | sed -n 's/^10.0.0.1/&/p'
echo "$TEST" | sed -n 's/^10.0.0.2$/&/p'
echo "$TEST" | sed -n 's/^10.0.0.21/&/p'
echo "$TEST" | sed -n 's/^10.0.0.3/&/p')

printf "$CAPTURE"

# 5  
Old 10-24-2010
Code:
#!/usr/xpg4/bin/sh
export PATH="/usr/xpg4/bin:$PATH"

TEST="10.0.0.1
10.0.0.2
10.0.0.21
10.0.0.3
10.0.0.4"

CAPTURE=$(echo "$TEST" | eval sed -n \
$(for ip in 10.0.0.1 10.0.0.2 10.0.0.21 10.0.0.3
do
  printf "-e 's/^%s$/&/p' " $ip
done))

echo "$CAPTURE"

or even
Code:
#!/usr/xpg4/bin/sh
export PATH="/usr/xpg4/bin:$PATH"

TEST="10.0.0.1
10.0.0.2
10.0.0.21
10.0.0.3
10.0.0.4"

CAPTURE=$(for ip in 10.0.0.1 10.0.0.2 10.0.0.21 10.0.0.3
do
  echo "$TEST" | grep -w $ip
done)

echo "$CAPTURE"

# 6  
Old 10-24-2010
Alternatively, without the linefeeds in the variables and no external commands:

shell code:
  1. #!/usr/xpg4/bin/sh
  2. export PATH="/usr/xpg4/bin:$PATH"
  3. TEST="10.0.0.1 10.0.0.2 10.0.0.21 10.0.0.3 10.0.0.4"
  4. IPS="10.0.0.1 10.0.0.2 10.0.0.21 10.0.0.3"
  5. for i in $TEST
  6. do
  7.   for j in $IPS
  8.   do
  9.     if [ "$i" = "$j" ]; then
  10.       CAPTURE="$CAPTURE $i"
  11.     fi
  12.   done
  13. done
  14. printf "%s\n" ${CAPTURE# }
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

2. Shell Programming and Scripting

Sending Sed/Echo output to Variable

I have a variable $WORDS that contains a string Then i want to use sed to break it up. echo $WORDS | sed 's// /g' I tried setting this as a variable by doing WORDS2=`echo $WORDS | sed 's// /g'` But when i do this it does not return me to the prompt properly ie. jmpprd-v1> jmpprd-v1>... (4 Replies)
Discussion started by: nitrobass24
4 Replies

3. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

4. Shell Programming and Scripting

Appending value to variable

I have a Query! by using command cat >> file1 we can append data's to a already existing file, Similarly is it possible to append a data to a variable. Thanks in Advance!! (2 Replies)
Discussion started by: gwgreen1
2 Replies

5. 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

6. Shell Programming and Scripting

Appending to a variable?

Hey, I'm creating a custom useradd script, and I'm giving the option to add secondary groups. Basically what I want to do is ask for the name of the group, you type in the group you want to add, it assigns that group name to the variable $sgroup. Then the scripts asks if you want add another. If... (0 Replies)
Discussion started by: paqman
0 Replies

7. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies

8. Shell Programming and Scripting

appending to sed output of one file into the middle of file

hi, i have a file file1 file2 ----------- ----------------- aa bbb ccc 111 1111 1111 ddd eee fff 222 3333 4444 ggg hhh... (5 Replies)
Discussion started by: go4desperado
5 Replies

9. Shell Programming and Scripting

Passing output of sed/echo to a variable

I understand how to use a variable in a sed command, but for the life of me I can't get the output into a variable. I'm making a general function to replace part of a filename with a different string, so: >>myscript this that would change: this_file001.txt to that_file001.txt and... (11 Replies)
Discussion started by: donflamenco
11 Replies
Login or Register to Ask a Question