27

What GNU/Linux command-line tool would I use for performing a search and replace on a file?

Can the search text, and replacement, be specified in a regex format?

4 Answers 4

54
sed 's/a.*b/xyz/g;' old_file > new_file

GNU sed (which you probably have) is even more versatile:

sed -r --in-place 's/a(.*)b/x\1y/g;' your_file

Here is a brief explanation of those options:

-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied)

-r, --regexp-extended use extended regular expressions in the script.

The FreeBSD, NetBSD and OpenBSD versions also supports these options.

If you want to learn more about sed, Cori has suggested this tutorial.

2
  • I didn't know about the -r switch to allow extended regex - handy! Also, -i is a shortcut for --in-place.
    – Sam Stokes
    Oct 1, 2008 at 2:17
  • And use cori's link above if you want to learn more. sed is more lightweight than perl afaik Oct 1, 2008 at 2:17
28

Perl was invented for this:

perl -pi -e 's/foo/bar/g;' *.txt

Any normal s/// pattern in those single quotes. You can keep a backup with something like this:

perl -pi.bak -e 's/foo/bar/g;' *.txt

Or pipeline:

cat file.txt | perl -ne 's/foo/bar/g;' | less

But that's really more sed's job.

6

Consider Ruby as an alternative to Perl. It stole most of Perl's one-liner commandline args (-i, -p, -l, -e, -n) and auto-sets $_ for you like Perl does and has plenty of regex goodness. Additionally Ruby's syntax may be more comfortable and easier to read or write than Perl's or sed's. (Or not, depending on your tastes.)

ruby -pi.bak -e '$_.gsub!(/foo|bar/){|x| x.upcase}' *.txt

vs.

perl -pi.bak -e 's/(foo|bar)/\U\1/g' *.txt

In many cases when dealing with one-liners, performance isn't enough of an issue to care whether you use lightweight sed or heavyweight Perl or heaveier-weight Ruby. Use whatever is easiest to write.

1
  • 1
    Thanks for the helpful example! I think it could be simplified (slightly) by: ruby -pi.bak -e 'gsub(/foo|bar/){|x| x.upcase}' *.txt (eg, removing the $_).
    – Colin K
    Oct 19, 2011 at 23:41
5

sed, the stream editor, and yes, it uses regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.