If I am doing several substitutions which need to be consecutive, e.g.

sed -i '/^[[:space:]]*browser.*\.should/s/browser/expect(browser/' t1_spec.rb
sed -i '/expect(browser.*\.should/s/\.should/).should/' t1_spec.rb
sed -i 's/\.should/\.to/' t1_spec.rb 
sed -i 's/==/eq/' t1_spec.rb 

Is there a better way to do this that will only go through the t1_spec.file once and do the the 4 substitutions for each line rather than going through the file 4 times?


Beside using semicolons, you can also give more than one expression to sed by using the -e flag:

sed -i -e 's/expr1/replace1/' -e 's/expr2/replace2/'  t1_spec.rb 

In GNU (e.g. on my Ubuntu machine), simply using multiple lines is supported and works well and looks good (imho) as it avoids super long lines, e.g.

sed -i '/^[[:space:]]*browser.*\.should/s/browser/expect(browser/
        s/==/eq/' t1_spec.rb 

You can use semicolons to separate the different substitions between the single quotes or you can put all those commands in a file and specify the file with the -f option to sed.

Using the file is particularly useful if you are going to use the same set of sed commands over and over again. I used to have to deal with a dirty data feed and I ended up over time with an 88 line file called massage.sed to automatically clear out most of the common mistakes I got from the feed.

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.