How to append contents of multiple files into one file

前端 未结 11 2082
既然无缘
既然无缘 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:21

    If all your files are in single directory you can simply do

    cat * > 0.txt

    Files 1.txt,2.txt, .. will go into 0.txt

    0 讨论(0)
  • 2020-12-22 16:21

    if your files contain headers and you want remove them in the output file, you can use:

    for f in `ls *.txt`; do sed '2,$!d' $f >> 0.out; done
    
    0 讨论(0)
  • 2020-12-22 16:22

    If the original file contains non-printable characters, they will be lost when using the cat command. Using 'cat -v', the non-printables will be converted to visible character strings, but the output file would still not contain the actual non-printables characters in the original file. With a small number of files, an alternative might be to open the first file in an editor (e.g. vim) that handles non-printing characters. Then maneuver to the bottom of the file and enter ":r second_file_name". That will pull in the second file, including non-printing characters. The same could be done for additional files. When all files have been read in, enter ":w". The end result is that the first file will now contain what it did originally, plus the content of the files that were read in.

    0 讨论(0)
  • 2020-12-22 16:24

    Another option, for those of you who still stumble upon this post like I did, is to use find -exec:

    find . -type f -name '*.txt' -exec cat {} + >> output.file
    

    In my case, I needed a more robust option that would look through multiple subdirectories so I chose to use find. Breaking it down:

    find .
    

    Look within the current working directory.

    -type f
    

    Only interested in files, not directories, etc.

    -name '*.txt'
    

    Whittle down the result set by name

    -exec cat {} +
    

    Execute the cat command for each result. "+" means only 1 instance of cat is spawned (thx @gniourf_gniourf)

     >> output.file
    

    As explained in other answers, append the cat-ed contents to the end of an output file.

    0 讨论(0)
  • 2020-12-22 16:25

    All of the (text-) files into one

    find . | xargs cat > outfile
    

    xargs makes the output-lines of find . the arguments of cat.

    find has many options, like -name '*.txt' or -type.

    you should check them out if you want to use it in your pipeline

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题