4 ms·
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
by rav 8y ago
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.
- oblio 8y agoYou can even: https://stackoverflow.com/a/23237529 https://stackoverflow.com/a/23237529
- JoshMnem 8y agoThanks 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
- teddyh 8y agoWhy 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’.
- JoshMnem 8y agoThanks 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.
- teddyh 8y agoNote: I forgot the “--max-args=1” option to xargs.