我正在使用cat *.txt将多个 txt 文件合并为一个文件,但我需要每个文件出现在一行上。
如何最好地合并文件,并使每个文件都出现在新的一行上?
只需使用awk
awk 'FNR==1{print ""}1' *.txt
awk 'FNR==1&&NR!=1{print ""}1' *.txt
。 - Scott Yang如果您有支持它的剪贴板
,
paste --delimiter=\\n --serial *.txt
做得非常好。
-d
和 -s
都是 POSIX 规定的,因此不应该有不支持它们的 paste
。这个解决方案真的需要更多的投票支持。 - kralykecho * | xargs paste -s -d \n > foo.txt
。 - Joshua Robinsonpaste -d=\\n -s *.chunk.txt > all.txt
可以。 - Per Quested Aronsson您可以使用for循环遍历每个文件:
for filename in *.txt; do
# each time through the loop, ${filename} will hold the name
# of the next *.txt file. You can then arbitrarily process
# each file
cat "${filename}"
echo
# You can add redirection after the done (which ends the
# for loop). Any output within the for loop will be sent to
# the redirection specified here
done > output_file
for file in *.txt
do
cat "$file"
echo
done > newfile
for file in *.txt
do
cat "$file" >> result
echo >> result
done
echo -n a > a.txt; echo -n b > b.txt; cat a.txt b.txt
。 - ephemientecho "一些文本" > text.txt; file text.txt; echo -n "一些文本" > text.txt; file text.txt
- Dennis Williamsoncat
不会在文件之间添加任何换行符。如果一个文件以换行符结尾(就像大多数UNIX文本文件那样),那很好;如果没有,那就不会自动添加。 - ephemient