Move lines matching a pattern from one file to another

后端 未结 5 1510
一向
一向 2021-02-12 23:09

I want to move lines matching certain pattern from file1 to file2. Analogous to operation cut and paste from one file to another in windows

5条回答
  •  没有蜡笔的小新
    2021-02-12 23:32

    You can use perl and select a different filehandle based in a match of a regular expression when printing:

    perl -i.bak -ne 'BEGIN { open $oh, q|>|, pop or die } { print { m/bar/ ? $oh : q|ARGVOUT| } $_ }' file1 file2
    

    It yields:

    ==> file1 <==
    bla foo bla
    bla aaa bla
    bla foo bla
    
    ==> file2 <==
    bla bar bla
    bla bar bla
    

提交回复
热议问题