linux - 如何连接具有相同标题的多个文件?有些文件只有标题

标签 linux bash concatenation

我有大量名为 0.file.csv、..、1000.file.csv 的文件。我需要连接文件,只保留第一个文件的标题,并去掉其余文件的标题。我想到的解决方案是:

sudo awk 'FNR==1 && NR!=1{next;}{print}' {0..1000}.file.csv > file.csv

但是,如果某些文件只有标题,则此解决方案不起作用。

样本输入是:

0.file.csv
person_id, dob, year, subject, degree
0,1984/12/01,2014,math,ms

1.file.csv
person_id, dob, year, subject, degree

2.file.csv
person_id, dob, year, subject, degree
200,1990/03/12,2015,physics,bs
201,1991/04/18,2015,math,ms

输出应该是:

person_id, dob, year, subject, degree
0,1984/12/01,2014,math,ms
200,1990/03/12,2015,physics,bs
201,1991/04/18,2015,math,ms

最佳答案

一个更简单的 awk 命令:

awk 'FNR>1 || NR==1' {0..1000}.file.csv

但这与您原来的完全相同(但不依赖于 next)。它产生了预期的输出,但我不明白为什么你的原件没有。 (当我尝试时确实如此。)

关于linux - 如何连接具有相同标题的多个文件?有些文件只有标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027867/

相关文章:

linux - Nginx + Nodejs + WordPress 路由

c - linux内核一步一步

python - uwsgi 在作为 systemd 服务运行时损坏管道

bash - bash 中的代码块用法 { }

arrays - Bash 数组分组元素

html - 使用 grunt js,如何将 <title> 动态添加到静态 html 文件?

Matlab cat() 函数 : index exceeds matrix dimensions

linux - 如何在 bash 运行时使用 $ 创建变量值

linux - 如何在包含/bin 路径但不包含 shbangs 的目录中查找文件?

vb.net - VB.NET中 "{0}"和 "&"有什么区别?