News for hackers. Stuff that matters.
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.