Linux:合并多个文件,每个文件都在一个新行上

时间:2010-04-05 02:47:35

标签: linux bash

我正在使用 cat * .txt 将多个txt文件合并为一个,但我需要将每个文件放在一个单独的行中。

将文件与出现在新行上的每个文件合并的最佳方法是什么?

5 个答案:

答案 0 :(得分:38)

只需使用awk

awk 'FNR==1{print ""}1' *.txt

答案 1 :(得分:23)

如果你有paste支持它,

paste --delimiter=\\n --serial *.txt

做得非常好

答案 2 :(得分:17)

您可以使用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

答案 3 :(得分:7)

for file in *.txt
do
  cat "$file"
  echo
done > newfile

答案 4 :(得分:7)

我假设你想在文件之间换行。

for file in *.txt
do
   cat "$file" >> result
   echo >> result
done