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

后端 未结 4 2074
南方客
南方客 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:36

    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.

提交回复
热议问题