在 Bash 中将多个文本文件连接成一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150614/
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
Concatenating multiple text files into a single file in Bash
提问by Yada
What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file?
将目录中的所有 *.txt 文件合并为一个大文本文件的最快和最实用的方法是什么?
Currently I'm using windows with cygwin so I have access to BASH.
目前我正在使用带有 cygwin 的 windows,所以我可以访问 BASH。
Windows shell command would be nice too but I doubt there is one.
Windows shell 命令也不错,但我怀疑是否有。
回答by Robert Greiner
This appends the output to all.txt
这会将输出附加到 all.txt
cat *.txt >> all.txt
This overwrites all.txt
这将覆盖 all.txt
cat *.txt > all.txt
回答by Chinmay Kanchi
Just remember, for all the solutions given so far, the shell decides the order in which the files are concatenated. For Bash, IIRC, that's alphabetical order. If the order is important, you should either name the files appropriately (01file.txt, 02file.txt, etc...) or specify each file in the order you want it concatenated.
请记住,对于迄今为止给出的所有解决方案,shell 决定文件连接的顺序。对于 Bash、IIRC,这是按字母顺序排列的。如果顺序很重要,您应该适当地命名文件(01file.txt、02file.txt 等...)或按照您希望的顺序指定每个文件。
$ cat file1 file2 file3 file4 file5 file6 > out.txt
回答by Greg Hewgill
The Windows shell command type
can do this:
Windows shell 命令type
可以执行此操作:
type *.txt >outputfile
Type type
command also writes file names to stderr, which are not captured by the >
redirect operator (but will show up on the console).
Typetype
命令还将文件名写入 stderr,>
重定向操作符不会捕获这些文件名(但会显示在控制台上)。
回答by Carl Norum
You can use Windows shell copy
to concatenate files.
您可以使用 Windows shellcopy
来连接文件。
C:\> copy *.txt outputfile
From the help:
从帮助:
To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).
要追加文件,请为目标指定单个文件,为源指定多个文件(使用通配符或 file1+file2+file3 格式)。
回答by Jacobe2169
Be careful, because none of these methods work with a large number of files. Personally, I used this line:
请小心,因为这些方法都不适用于大量文件。就个人而言,我使用了这一行:
for i in $(ls | grep ".txt");do cat $i >> output.txt;done
EDIT: As someone said in the comments, you can replace $(ls | grep ".txt")
with $(ls *.txt)
编辑:正如有人在评论中所说,您可以替换$(ls | grep ".txt")
为$(ls *.txt)
EDIT: thanks to @gnourf_gnourf expertise, the use of globis the correct way to iterate over files in a directory. Consequently, blasphemous expressions like $(ls | grep ".txt")
must be replaced by *.txt
(see the article here).
编辑:感谢@gnourf_gnourf 的专业知识,使用glob是迭代目录中文件的正确方法。因此,像这样的亵渎神明的表达$(ls | grep ".txt")
必须被替换为*.txt
(请参阅此处的文章)。
Good Solution
好的解决方案
for i in *.txt;do cat $i >> output.txt;done
回答by ghostdog74
the most pragmatic way with the shell is the cat command. other ways include,
使用 shell 最实用的方法是 cat 命令。其他方式包括,
awk '1' *.txt > all.txt
perl -ne 'print;' *.txt > all.txt
回答by GPrathap
How about this approach?
这种方法怎么样?
find . -type f -name '*.txt' -exec cat {} + >> output.txt
回答by Ori
type [source folder]\*.[File extension] > [destination folder]\[file name].[File extension]
For Example:
例如:
type C:\*.txt > C:\all.txt
That will Take all the txt files in the C:\ Folder and save it in C:\1 Folder by the name of all.txt
这将把 C:\ 文件夹中的所有 txt 文件以 all.txt 的名称保存在 C:\1 文件夹中
Or
或者
type [source folder]\* > [destination folder]\[file name].[File extension]
For Example:
例如:
type C:\* > C:\all.txt
That will take all the files that are present in the folder and put there Content in C:\1\all.txt
这将把文件夹中存在的所有文件放在 C:\1\all.txt 中
回答by Michael-zhang
You can do like this:
cat [directory_path]/**/*.[h,m] > test.txt
你可以这样做:
cat [directory_path]/**/*.[h,m] > test.txt
if you use {}
to include the extension of the files you want to find, there is a sequencing problem.
如果使用{}
包含要查找的文件的扩展名,则存在排序问题。
回答by leo
When you run into a problem where it cats all.txt into all.txt, You can try check all.txt is existing or not, if exists, remove
当您遇到将 all.txt 转换为 all.txt 的问题时,您可以尝试检查 all.txt 是否存在,如果存在,则删除
Like this:
像这样:
[ -e $"all.txt" ] && rm $"all.txt"
[ -e $"all.txt" ] && rm $"all.txt"