简体   繁体   English

bash猫多档

[英]bash cat multiple files

I am trying to cat three files and obtain and insert a newline \n after 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?实现这一目标的最简单方法是什么?

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

或者

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

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只要你cat的文件,会出现在它们之间没有EOF,并没有其他的方式找到边界,所以我建议像for file in f1 f2 f3; do cat $file; echo; done for file in f1 f2 f3; do cat $file; echo; done 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或缩进,

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

EOF isn't a character, not even CTRL-D - that's just the usual terminal method for indicating EOF on interactive input. EOF不是字符,甚至不是 CTRL-D - 这只是在交互式输入中指示 EOF 的常用终端方法。 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来做这件事:-)

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

我遇到了类似的问题,对我来说最有效的方法是:

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

If you are not tight to cat tool only, use bat https://github.com/sharkdp/bat .如果您不只使用cat工具,请使用bat https://github.com/sharkdp/bat

bat -p file1 file2 file3

also, if you have dynamic files count, one can use wildcard:此外,如果您有动态文件数,可以使用通配符:

bat -p *.extension

尝试这个:

find f1 f2 f3 | xargs cat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM