Linux: Merging multiple files, each on a new line

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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

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

回答1:

just use awk

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


回答2:

If you have a paste that supports it,

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

does a really great job



回答3:

You can iterate through each file with a for loop:

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:

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


回答5:

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!