Unix:通过保留第一个文件的标题,合并具有相同标头的多个CSV文件 [英] Unix:merge multiple CSV files with same header by keeping the header of the first file

查看:359
本文介绍了Unix:通过保留第一个文件的标题,合并具有相同标头的多个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须合并多个含有相同标题的CSV档案。
我必须保留第一个文件的标题,并删除所有其他文件的标题,并合并它们并创建一个主文件。

I have to merge multiple CSV files with same headers. I have to keep the header of the first file and remove headers of all the other files and merge them and create one master file.

文件1: / p>

file 1:

Id,city,name ,location
1,NA,JACK,CA

档案2:

ID,city,name,location
2,NY,JERRY,NY



output:

Id,city,name,location
1,NA,JACK,CA
2,NY,JERRY,NY

目前我正在使用此代码:

Currently I am using this code:

ls *.csv | xargs -n 1 tail -n+2 > master.csv

这段代码将合并文件,但是我需要第一个文件的头

This code will merge the files perfectly , but as I need the header of the first file, this will not give me the header.

推荐答案

>

awk 'FNR==1 && NR!=1{next;}{print}' *.csv


> cat file1.csv
Id,city,name ,location
1,NA,JACK,CA
>
> cat file2.csv
ID,city,name,location
2,NY,JERRY,NY
>
> nawk 'FNR==1 && NR!=1{next;}{print}' *.csv
Id,city,name ,location
1,NA,JACK,CA
2,NY,JERRY,NY
> 

这篇关于Unix:通过保留第一个文件的标题,合并具有相同标头的多个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆