11

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

3 Answers 3

8

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

3

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

1
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .