Move lines matching a pattern from one file to another

后端 未结 5 1413
一向
一向 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:44

    This awk script will do the trick:

    awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "file"(a[i]~/bar/?2:1)}' file1
    

    Outputs:

    $ cat file1
    bla foo bla
    bla aaa bla
    bla foo bla
    
    $ cat file2
    bla bar bla
    bla bar bla
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题