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

后端 未结 4 2077
南方客
南方客 2021-02-07 00:10

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条回答
  •  情深已故
    2021-02-07 00:49

    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.

提交回复
热议问题