In-place processing with grep

前端 未结 9 2110
庸人自扰
庸人自扰 2020-12-25 11:53

I\'ve got a script that calls grep to process a text file. Currently I am doing something like this.

$ grep \'SomeRegEx\' myfile.txt > myfile.txt.temp
$ m         


        
9条回答
  •  死守一世寂寞
    2020-12-25 12:25

    In general, this can't be done. But Perl has the -i switch:

    perl -i -ne 'print if /SomeRegEx/' myfile.txt
    

    Writing -i.bak will cause the original to be saved in myfile.txt.bak.

    (Of course internally, Perl just does basically what you're already doing -- there's no special magic involved.)

提交回复
热议问题