在Linux中,批量合并文本文件可以通过多种命令行工具来实现,如cat
、paste
、awk
等。以下是一些常用的方法:
cat
命令cat
命令可以用来连接文件并打印到标准输出设备。
cat file1.txt file2.txt file3.txt > combined.txt
这条命令会将file1.txt
、file2.txt
和file3.txt
的内容合并到一个名为combined.txt
的新文件中。
paste
命令paste
命令可以将多个文件的行并排放置。
paste file1.txt file2.txt > combined.txt
这会将file1.txt
和file2.txt
的每一行并排放置在combined.txt
中。
awk
命令awk
是一个强大的文本处理工具,可以用来合并文件。
awk 'FNR==NR{a[FNR]=$0;next}{print a[FNR],$0}' file1.txt file2.txt > combined.txt
这条命令会将file1.txt
的内容作为第一列,file2.txt
的内容作为第二列,合并到combined.txt
中。
如果你有多个文件需要合并,可以使用循环来简化命令。
for file in *.txt; do cat "$file"; done > combined.txt
这条命令会将当前目录下所有的.txt
文件内容合并到combined.txt
中。
通过上述方法,你可以根据不同的需求选择合适的命令来批量合并文本文件。
领取专属 10元无门槛券
手把手带您无忧上云