Multiple file editing

I need to edit some 150 text files with the same edit. To change “foobar” to “barfoo”. Is there a simple way to do that?

Thanks in advance.

Take a look at “kfilereplace”…

https://software.opensuse.org/package/kfilereplace?search_term=kfilereplace

Lots of solutions (sed, awk, ex, etc). I like ex (scripted vim).

Remember backup all files before attempting.

Also, if you mess-up your script it can create .swp files (e.g. .test.txt.swp) and you will wonder why your script never runs after you fix it.


#!/bin/bash

for i in test*.txt
do
ex $i<<HERE 
:%s/foobar/barfoo/g
:x
HERE
done

Thank you very much. It’s simple, elegant and super fast. It’s a keeper.

If that was the actual change then using sed is simpler, probably faster,
and will create the backups for you:


sed -i-backup-$(date +%s) -e 's/foober/parfoo/g' test*.txt

This modifies test*.txt files, doing the search/replace globally (meaning
multiple times per line, if applicable), but creating a file ending in
FILENAME-backup-1527597163 (or close on those numbers) first. Why the big
string of numbers? Because if you happen to run the command above, and
then run it a second time, you’ll overwrite the original backups if you
are not careful, and that may be sad.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below.

If you want to send me a private message, please let me know in the
forum as I do not use the web interface often.