bash cat multiple files

落花浮王杯 提交于 2019-12-18 15:29:29

问题


I am trying to cat three files and obtain and insert a newline \n after each file ,I thought of using something like :

cat f1 f2 f3|tr "\EOF" "\n"

without success.

What is the easiest way to achieve that ?


回答1:


cat f1 <(echo) f2 <(echo) f3 <(echo) 

or

perl -pe 'eof&&s/$/\n/' a b c



回答2:


As soon as you cat the files, there will be no EOF in between them, and no other way to find the border, so I'd suggest something like for file in f1 f2 f3; do cat $file; echo; done or, with indentation,

for file in f1 f2 f3; do
    cat $file;
    echo;
done



回答3:


EOF isn't a character, not even CTRL-D - that's just the usual terminal method for indicating EOF on interactive input. So you can't use tools for translating characters to somehow modify it.

For this simple case, the easiest way is to stop worrying about trying to do it in a single cat :-)

cat f1; echo; cat f2; echo; cat f3

will do the trick just fine. Any larger number of files may be worthy of a script but I can't see the necessity for this small case.

If you want to combine all those streams for further processing, simply run them in a subshell:

( cat f1; echo; cat f2; echo; cat f3 ) | some_process



回答4:


i was having a similar problem, what worked best for me i my situation was:

grep "" file1 file2 file3 | awk -F ':' '{print $2}'



回答5:


Try this:

find f1 f2 f3 | xargs cat


来源:https://stackoverflow.com/questions/12037634/bash-cat-multiple-files

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