Edit files using ex


Short note #2

I had a lot of text files (Markdown as it happens) which all needed exactly the same edit.
It was a simple edit, go to line 4, join it with next line and remove a couple of characters.
Very tedious to do manually, enter ex.

$ find . -name \*.md -exec ex -s +5 -c - -c j -c "s/\" T08/T08/" -cx {} \; -print

The find gathered up all the files with md extension and ran ex against each one, printing the filename as it went.
ex options are as follows:

  -s    Silent mode
  +5    Go to line 5
  -c -  Move up one line ('course could just have gone to line 4)
  -c j  Join line with next one
  -c "s Search and replace
  -cx   Save and exit

There can be up to 10 commands (using -c) on the command line.

It would have been possible to do the same thing using either sed or awk but using ex is nice and simple.

Comment on this article using form below. Requires email login only for authentication. HTML forbidden, Markdown only.