tags:

views:

173

answers:

4

This script uses sed to change all "" to "new stuff". How would one change just the "" after the yyy: using sed or anything else?

cat >sample.txt <<EOF
xxx:
""
yyy:
""
}
EOF
sed --expression='s/""/"new stuff"/' sample.txt
A: 

I find once things get beyond a certain level of complexity, I switch to perl. s2p will handle the translation of your current sed solution. Or you could write it from scratch trivially. The search/replace expression will remain the same.

Brian Agnew
+1  A: 

Answer gleaned from O'Reilly - Sed & Awk 2nd Addition Around page 152

Write a script in a file

/yyy\:$/{
N
s/yyy\:\n\"\"/yyy\:\
\"new stuff\"/
}

Apply this to your data with the usual

sed -f script sample.txt

This script says look for yyy:. When found read another line into the pattern buffer (sounds like Star Trek Transporter). Now do a s/ command on the joined lines. So we are looking for yyy: newline "". If found replace with yyy: \ notice backslash actual newline then "new stuff"

Good luck

see man page:http://unixhelp.ed.ac.uk/CGI/man-cgi?sedsearch for addresses paragraph
SashaN
+2  A: 

I might not understand your question. If you want to replace ONLY the value after 'yyy' then use the previous answer. If you want to replace ANY values after 'yyy', try this one-liner:

sed --expression='/yyy/,$ s/""/"new stuff"/g' sample.txt

Haven't tested it :D...

noluckmurphy
A: 

i am confused man

kiki