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

Mar*_*rco 25 linux bash

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

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

gho*_*g74 38

只需使用awk

awk 'FNR==1{print ""}1' *.txt
Run Code Online (Sandbox Code Playgroud)

  • 建议以下编辑在结果的顶部不包括换行符:`awk'FNR == 1 && NR!= 1 {print""} 1'*.txt` (2认同)

Suc*_*uch 23

如果你有paste支持它,

paste --delimiter=\\n --serial *.txt
Run Code Online (Sandbox Code Playgroud)

做得非常好

  • 这是我测试过的最快的文件串联方法之一.很好的主意... (2认同)
  • 神圣的哎呀! (2认同)

R S*_*hko 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
Run Code Online (Sandbox Code Playgroud)


Ign*_*ams 7

for file in *.txt
do
  cat "$file"
  echo
done > newfile
Run Code Online (Sandbox Code Playgroud)


Mat*_*hen 7

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

for file in *.txt
do
   cat "$file" >> result
   echo >> result
done
Run Code Online (Sandbox Code Playgroud)