Unix:merge multiple CSV files with same header by keeping the header of the first file

后端 未结 4 1476
清酒与你
清酒与你 2020-12-01 00:35

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 mas

4条回答
  •  时光说笑
    2020-12-01 01:00

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

    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
    > 
    

    Explanation given by kevin-d:

    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.

    Link for the difference between awk and nawk

提交回复
热议问题