Move lines matching a pattern from one file to another

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

    You can put a && between the two commands to make them a single line. But that won't be more readable, so I don't recommend you do that.

    To do the same in one command, you would need something that can edit a file in-place, removing lines you don't want and at the same time printing those lines to stdout or stderr so you could redirect it to the other file. Maybe ed can do this but I don't know how to write that. I don't think there is another "standard" UNIX tool to do this.

    Btw, cut & paste actually has 3 steps:

    1. Copy selected text to clipboard
    2. Remove from original file
    3. Paste from clipboard to new file

    The 2-step UNIX command does it without a clipboard.

提交回复
热议问题