How to append contents of multiple files into one file

前端 未结 11 2132
既然无缘
既然无缘 2020-12-22 15:49

I want to copy the contents of five files to one file as is. I tried doing it using cp for each file. But that overwrites the contents copied from the previous file. I also

11条回答
  •  轮回少年
    2020-12-22 16:26

    Another option is sed:

    sed r 1.txt 2.txt 3.txt > merge.txt 
    

    Or...

    sed h 1.txt 2.txt 3.txt > merge.txt 
    

    Or...

    sed -n p 1.txt 2.txt 3.txt > merge.txt # -n is mandatory here
    

    Or without redirection ...

    sed wmerge.txt 1.txt 2.txt 3.txt
    

    Note that last line write also merge.txt (not wmerge.txt!). You can use w"merge.txt" to avoid confusion with the file name, and -n for silent output.

    Of course, you can also shorten the file list with wildcards. For instance, in case of numbered files as in the above examples, you can specify the range with braces in this way:

    sed -n w"merge.txt" {1..3}.txt
    

提交回复
热议问题