I'm a little rusty in my sed and haven't used it much since moving to a Mac but I am aware there seems to be a difference with what sed is used on a Mac per Ubuntu after a few searches. What I am trying to do is replace the first part on an XML file with some new values. After refreshing my knowledge on sed I decided to use:

sed -e '1,/<\/value>/ s/.*/\<something\>lorem ipsum\<\/something\>\n\<again\>wednesday\<\/again\>\n/' $theFile

when I run the code it outputs <something>lorem ipsum</something>n<again>wednesday</again>n on all the lines from the 1st line up to where </value> ends.

So my question is what is the appropriate way to replace the contents of the file on its desired new line without duplicating it on every line?


The sed is line-editor so it apply script to each line wich addressed. So you should collect lines which you'd like to exchange in one string with newline separator(s):

sed -e '1{:1;N;/<\/value>/b1;s/.*/\<something\>lorem.....\n/}' $theFile

Other way is to insert necessary text before block then remove the block:

sed -e '1i\\<something\>lorem.....\n' -e '1,/<\/value>/d' $theFile
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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