News for hackers. Stuff that matters.
Re:Batch editing files with ed by rav on Saturday May 12, 2018 @01:45PM (#17055140)
In practice I often use Vim instead.

    :args file1.txt file2.txt file3.txt
    :set autowrite
    :argdo norm /- baz/<CR>yypwCelephants
The commands set the argument list to a list of three files (by default, it is set to the filenames you passed to vim on the command-line). Then, autowrite is enabled which automatically saves each buffer after editing it. Finally, argdo runs a command on each argument file.
Re:Batch editing files with ed by oblio on Saturday May 12, 2018 @02:40PM (#17055388)
Re:Batch editing files with ed by JoshMnem on Saturday May 12, 2018 @05:12PM (#17056035)
Thanks for that tip. I've used ex for similar things when editing hundreds of files.

This example searches each HTML file in a directory for a line with a string and then deletes a number of lines:

    $ echo "g/search string/ .,+20 d\nx" >> exscript
    $ for f in *.html
      do
          ex - $f < exscript
      done
Re:Batch editing files with ed by teddyh on Saturday May 12, 2018 @05:24PM (#17056089)
Why write the ‘ex’ commands to a file? Why not just do the echo inline, like this

   for f in *.html; do
       echo -e "g/search string/ .,+20 d\nx" | ex - "$f"
    done
?

(I also added quotes to the $f dereference in case of file names containing white space, and the -e flag to echo to expand \n to newline. In case of a Bourne shell without support for -e in echo, I would probably use “{ echo "g/..."; echo "x"; } | ex - ...” instead of using \n.)

Also, in a production script I would probably have used “find . -maxdepth 1 -name "*.html" -print0 | xargs --null --no-run-if-empty | while read f; do ...; done” instead of a ‘for’ loop from a pathname expansion, in order to guard against there being no html files, in which case a ‘for’ loop from a pathname expansion otherwise would be passing the literal string “*.html” as the file name argument to ‘ex’.

Re:Batch editing files with ed by JoshMnem on Saturday May 12, 2018 @11:12PM (#17057192)
Thanks for the tips. I put them in a file, because I saw someone using ed last year in a script and started looking at ex from there.

    diff -e file1 file2 > ed_script
Using echo (along with your other suggestions) is probably better for that example.
Re:Batch editing files with ed by teddyh on Sunday May 13, 2018 @07:50AM (#17058471)
Note: I forgot the “--max-args=1” option to xargs.