-2

(I'm sure this has been asked before, but I can only find somewhat-similar questions.)

I have bunch of files, some of which contain the string "foobar". I would like to replace, in each of those files, the occurrences of "foobar" with "baz" - in those files, not in new files or streamed anywhere.

I'd like to specify the filenames on the command-line, i.e.

[me@myhost:/$] magic_replace "foobar" "baz" file1 file2 file3 etc
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    Check `sed -i`. For example here you can find a good answer: http://stackoverflow.com/a/5171935/1983854 – fedorqui May 01 '14 at 11:20

2 Answers2

2

use sed.

sed -i 's/foobar/baz/g' file1 file2 file3 etc
yejinxin
  • 586
  • 1
  • 3
  • 13
1

Try this:

sed -i -e 's/foo/baz/g' file1 file2

or this:

for file in $(echo file1 file2); do
    sed -i -e 's/foo/baz/g' $file
done

The first one should work if sed accepts multiple input files, check it.

R.Sicart
  • 671
  • 3
  • 10