Question

(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
Was it helpful?

Solution 2

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.

OTHER TIPS

use sed.

sed -i 's/foobar/baz/g' file1 file2 file3 etc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top