Unix:通过保留第一个文件的标题来合并多个具有相同标题的 CSV 文件 [英] Unix:merge multiple CSV files with same header by keeping the header of the first file
问题描述
我必须合并多个具有相同标题的 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:
Id,city,name ,location
1,NA,JACK,CA
文件 2:
ID,city,name,location
2,NY,JERRY,NY
输出:
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
在solaris unix上测试:
tested on solaris unix:
> 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
>
kevin-d给出的解释:
FNR 是目前在当前文件中读取的行(记录)数.NR 是整体读取的行数.所以条件 'FNR==1 &&NR!=1{next;}' 说,如果它是第一行,请跳过这一行当前文件,并且至少整体读取了 1 行."打印第一个文件的 CSV 标头时跳过它的效果其他的.
FNR is the number of lines (records) read so far in the current file. NR is the number of lines read overall. So the condition 'FNR==1 && NR!=1{next;}' says, "Skip this line if it's the first line of the current file, and at least 1 line has been read overall." This has the effect of printing the CSV header of the first file while skipping it in the rest.
这篇关于Unix:通过保留第一个文件的标题来合并多个具有相同标题的 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!