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
link|improve this question

38% accept rate
feedback

migrated from stackoverflow.com Feb 12 '11 at 20:21

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 7 down vote accepted

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

link|improve this answer
feedback

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

link|improve this answer
1  
see man page: unixhelp.ed.ac.uk/CGI/man-cgi?sed search for addresses paragraph – SashaN Jun 16 '09 at 20:55
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.