Linux 连接文件并在文件之间插入新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8183191/
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-08-06 02:21:45  来源:igfitidea点击:

Concatenating Files And Insert New Line In Between Files

linuxunixcat

提问by neversaint

I have multiple files which I want to concat with cat. Let's say

我有多个文件要与cat. 让我们说

File1.txt 
foo

File2.txt
bar

File3.txt
qux

I want to concat so that the final file looks like:

我想连接以便最终文件看起来像:

foo

bar

qux

Instead of this with usual cat File*.txt > finalfile.txt

而不是这与通常 cat File*.txt > finalfile.txt

foo
bar 
qux

What's the right way to do it?

正确的做法是什么?

采纳答案by codaddict

You can do:

你可以做:

for f in *.txt; do (cat "${f}"; echo) >> finalfile.txt; done

Make sure the file finalfile.txtdoes not exist before you run the above command.

finalfile.txt在运行上述命令之前,请确保该文件不存在。

If you are allowed to use awkyou can do:

如果您被允许使用,awk您可以:

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

回答by Flexo

If it were me doing it I'd use sed:

如果是我这样做,我会使用 sed:

sed -e '$s/$/\n/' -s *.txt > finalfile.txt

In this sed pattern $ has two meanings, firstly it matches the last line number only (as a range of lines to apply a pattern on) and secondly it matches the end of the line in the substitution pattern.

在这个 sed 模式中 $ 有两个含义,首先它只匹配最后一行号(作为应用模式的行范围),其次它匹配替换模式中的行尾。

If your version of sed doesn't have -s(process input files separately) you can do it all as a loop though:

如果您的 sed 版本没有-s(单独处理输入文件),您可以将其全部作为循环执行:

for f in *.txt ; do sed -e '$s/$/\n/' $f ; done > finalfile.txt

回答by Robert Tupelo-Schneck

If you have few enough files that you can list each one, then you can use process substitutionin Bash, inserting a newline between each pair of files:

如果您有足够的文件来列出每个文件,那么您可以在 Bash 中使用进程替换,在每对文件之间插入一个换行符:

cat File1.txt <(echo) File2.txt <(echo) File3.txt > finalfile.txt

回答by lbrutti

That's how I just did it on OsX 10.10.3

这就是我在 OsX 10.10.3 上所做的

for f in *.txt; do (cat $f; echo '') >> fullData.txt; done

since the simple 'echo' command with no params ended up in no new lines inserted.

因为没有参数的简单“echo”命令最终没有插入新行。

回答by user3780389

This works in Bash:

这适用于 Bash:

for f in *.txt; do cat $f; echo; done

In contrast to answers with >>(append), the output of this command can be piped into other programs.

>>(append) 的答案相反,此命令的输出可以通过管道传输到其他程序中。

Examples:

例子:

  • for f in File*.txt; do cat $f; echo; done > finalfile.txt
  • (for ... done) > finalfile.txt(parens are optional)
  • for ... done | less(piping into less)
  • for ... done | head -n -1(this strips off the trailing blank line)
  • for f in File*.txt; do cat $f; echo; done > finalfile.txt
  • (for ... done) > finalfile.txt(括号是可选的)
  • for ... done | less(管道少)
  • for ... done | head -n -1(这会去掉尾随的空白行)

回答by user3780389

In python, this concatenates with blank lines between files (the ,suppresses adding an extra trailing blank line):

在 python 中,这与文件之间的空行连接(,抑制添加额外的尾随空行):

print '\n'.join(open(f).read() for f in filenames),

Here is the ugly python one-liner that can be called from the shell and prints the output to a file:

这是丑陋的 python one-liner,可以从 shell 调用并将输出打印到文件:

python -c "from sys import argv; print '\n'.join(open(f).read() for f in argv[1:])," File*.txt > finalfile.txt

回答by Nick Roz

You may do it using xargsif you like, but the main idea is still the same:

xargs如果你愿意,你可以使用它,但主要思想仍然相同:

find *.txt | xargs -I{} sh -c "cat {}; echo ''" > finalfile.txt