Using ex to bulk-edit files

I previously wrote about command line scripting with ed and ex. I just had an opportunity to put ex into use in practice.

I needed to edit hundreds of files, searching for a line of text and then deleting it along with the next 22 lines.

I created an exscript file containing this code:

  1. g/block_2/ .,+22 d
  2. x

Then ran it on a file to test it:

  1. $ ex - index.html < exscript

Once I confirmed that it worked correctly, I ran it on the entire directory:

  1. for f in *.html
  2. do
  3. ex - $f < exscript
  4. done

This solution worked perfectly for my task, which was to remove a block of text from every page of a website downloaded with wget.