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

时间:2021-01-14 20:30:29

I am using cat *.txt to merge multiple txt files into one, but I need each file to be on a separate line.

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

What is the best way to merge files with each file appearing on a new line?

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

5 个解决方案

#1


36  

just use awk

只需使用awk

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

#2


23  

If you have a paste that supports it,

如果你有一个支持它的粘贴,

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

does a really great job

做得非常棒

#3


17  

You can iterate through each file with a for loop:

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

#4


7  

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

#5


6  

I'm assuming you want a line break between files.

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

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

#1


36  

just use awk

只需使用awk

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

#2


23  

If you have a paste that supports it,

如果你有一个支持它的粘贴,

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

does a really great job

做得非常棒

#3


17  

You can iterate through each file with a for loop:

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

#4


7  

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

#5


6  

I'm assuming you want a line break between files.

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

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