bash cat 多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12037634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:04:33  来源:igfitidea点击:

bash cat multiple files

linuxbashshellcat

提问by Lucian Enache

I am trying to catthree files and obtain and insert a newline \nafter each file ,I thought of using something like :

我正在尝试cat三个文件并\n在每个文件后获取并插入一个换行符,我想使用类似的东西:

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

without success.

没有成功。

What is the easiest way to achieve that ?

实现这一目标的最简单方法是什么?

回答by Nahuel Fouilleul

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

or

或者

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

回答by paxdiablo

EOFisn'ta 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.

EOF不是字符,甚至不是 CTRL-D - 这只是在交互式输入中指示 EOF 的常用终端方法。所以你不能使用翻译字符的工具来修改它。

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

对于这个简单的情况,最简单的方法是不要担心尝试在一个单一的情况下完成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:

如果您想组合所有这些流以进行进一步处理,只需在子 shell 中运行它们:

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

回答by Qnan

As soon as you catthe 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; doneor, with indentation,

一旦你找到cat文件,它们之间就没有 EOF,也没有其他方法可以找到边界,所以我建议使用类似for file in f1 f2 f3; do cat $file; echo; done或缩进的方法,

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

回答by cptPH

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

我遇到了类似的问题,最适合我的方法是:

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

回答by Dmitry Dubovitsky

Try this:

尝试这个:

find f1 f2 f3 | xargs cat