Quantcast
Channel: Transpose few columns from a csv file based on several columns using awk - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 2

Transpose few columns from a csv file based on several columns using awk

$
0
0

In my previous tag, i would keeping csv columns using a awk generic code. The answer was given here by @steeldriver.

In the following, I want to transpose some columns from a csv file to get a new column.

The description of data is:

  1. In the input, the first line contains the countries(several fields are empty) and the second line contains the headers to be processed.

  2. A transposing columns start from the last empty column based on the countries. Not that the number of columns per country vary. There can be 2 or 20 countries columns.

Example:

  • Input: file.csv
    ,,,fr,chnum,nom,date reg,match flag_fr,match flag_ch0001,AA,2020-05-15,reg1_fr,reg2_ch0002,AAA,2020-05-20,,reg3_ch
  • Ouput: file1.csv
    num,nom,date reg,match flag,country0001,AA ,2020-05-15,reg1_fr,fr      0002,AAA,2020-05-20,   _fr,fr       0001,AA ,2020-05-15,reg2_ch,ch0002,AAA,2020-05-20,reg3_ch,ch

I used the following code, the result is correct, but it forces me to declare all the different fields manually. Example:

awk -F, '    BEGIN{OFS=FS}     NR==2{n = split($0,a); print "num,nom,date reg,match country,flag"; next}    {for(i=4;i<=NF;i++) if (a[i] !=""){ print $1,$2,$3,a[i],$i} }'< file.csv>file1.csv

Resulting file1.csv

num,nom,date reg,match country,flag0001,AA,2020-05-15,match flag_fr,reg1_fr0001,AA,2020-05-15,match flag_ch,reg2_ch0002,AAA,2020-05-20,match flag_fr,0002,AAA,2020-05-20,match flag_ch,reg3_ch

How can I do this, using generic awk code if possible?


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>