Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

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

share|improve this answer
add comment

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

share|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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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