Linux 如何将多个文件的内容附加到一个文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18006581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:26:51  来源:igfitidea点击:

How to append contents of multiple files into one file

linuxbashunix

提问by Steam

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 tried

我想按原样将五个文件的内容复制到一个文件中。我尝试对每个文件使用 cp 来做到这一点。但这会覆盖从前一个文件复制的内容。我也试过

paste -d "\n" 1.txt 0.txt

and it did not work.

它没有用。

I want my script to add the newline at the end of each text file.

我希望我的脚本在每个文本文件的末尾添加换行符。

eg. Files 1.txt, 2.txt, 3.txt. Put contents of 1,2,3 in 0.txt

例如。文件 1.txt、2.txt、3.txt。将 1,2,3 的内容放入 0.txt

How do I do it ?

我该怎么做 ?

采纳答案by radical7

You need the cat(short for concatenate) command, with shell redirection (>) into your output file

您需要cat(concatenate 的缩写)命令,将 shell 重定向 ( >) 带入您的输出文件

cat 1.txt 2.txt 3.txt > 0.txt

回答by mopo922

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

对于那些仍然像我一样偶然发现这篇文章的人,另一种选择是使用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. 分解它:

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 catis spawned (thx @gniourf_gniourf)

对每个结果执行 cat 命令。"+" 表示只cat产生1 个实例(thx @gniourf_gniourf)

 >> output.file

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

如其他答案中所述,将 cat-ed 内容附加到输出文件的末尾。

回答by Eswar Yaganti

if you have a certain output type then do something like this

如果你有某种输出类型,那么做这样的事情

cat /path/to/files/*.txt >> finalout.txt

回答by Pooja

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

如果你的所有文件都在一个目录中,你可以简单地做

cat * > 0.txt

cat * > 0.txt

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

文件 1.txt,2.txt, .. 将进入 0.txt

回答by Ajit K'sagar

If you want to append contents of 3 files into one file, then the following command will be a good choice:

如果要将 3 个文件的内容附加到一个文件中,那么以下命令将是一个不错的选择:

cat file1 file2 file3 | tee -a file4 > /dev/null

It will combine the contents of all files into file4, throwing console output to /dev/null.

它将所有文件的内容合并到 file4 中,将控制台输出抛出到/dev/null.

回答by freddythunder

for i in {1..3}; do cat "$i.txt" >> 0.txt; done

I found this page because I needed to join 952 files together into one. I found this to work much better if you have many files. This will do a loop for however many numbers you need and cat each one using >> to append onto the end of 0.txt.

我找到这个页面是因为我需要将 952 个文件合并为一个。如果你有很多文件,我发现这会更好地工作。这将对您需要的任何数字进行循环,并使用 >> 将每个数字添加到 0.txt 的末尾。

回答by B.A.Cooper

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.

如果原始文件包含不可打印字符,使用 cat 命令时会丢失。使用“cat -v”,不可打印字符将被转换为可见字符串,但输出文件仍不会包含原始文件中实际的不可打印字符。对于少量文件,替代方法可能是在处理非打印字符的编辑器(例如 vim)中打开第一个文件。然后移动到文件底部并输入“:r second_file_name”。这将拉入第二个文件,包括非打印字符。对其他文件也可以这样做。读入所有文件后,输入“:w”。最终结果是第一个文件现在将包含它最初所做的,以及读入的文件的内容。

回答by Fran

Another option is sed:

另一种选择是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 -nfor silent output.

请注意,最后一行也写merge.txt(不是wmerge.txt!)。您可以使用w"merge.txt"避免与文件名混淆,并-n用于静默输出。

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

回答by brunocrt

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

如果您的文件包含标题并且您想在输出文件中删除它们,您可以使用:

for f in `ls *.txt`; do sed '2,$!d' $f >> 0.out; done

回答by AeaRy

If all your files are named similarly you could simply do:

如果您的所有文件都以类似方式命名,您可以简单地执行以下操作:

cat *.log >> output.log