2

I have several files named has follow (I just give 2 names as examples)(note: they have a semi colon ; field separator):

component1_component2_component3.csv

e.g.

almourol_gfdl_85.csv
almourol_had_85.csv

Each of this tables are organize as follow:

YEAR;MONTH;DAY;WFDisi;SWIM;GAP3;HYPE;VIC
1971;01;01;1206.1;627;1252.6;909;357.6
1971;01;02;1303.4;654.3;1397.4;916;478.5
1971;01;03;1248.9;662;1318.8;951;653.1

The first three columns are always the same but the number of columns can change and sometimes the tables are as follow:

YEAR;MONTH;DAY;ECOMAG;HYPE
1971;01;01;4138.3;4390
1971;01;02;4129;4380
1971;01;03;4115.9;4370

For all those tables I would like to change the header by implementing the second component of the file name. So for the two "real life" example here enclosed the desired output should look like this:

example1: - file name: almourol_gfdl_85.csv - desired output:

YEAR;MONTH;DAY;GFDL_WFDisi;GFDL_SWIM;GFDL_GAP3;GFDL_HYPE;GFDL_VIC
1971;01;01;1206.1;627;1252.6;909;357.6
1971;01;02;1303.4;654.3;1397.4;916;478.5
1971;01;03;1248.9;662;1318.8;951;653.1

example2: - file name: almourol_had_85.csv - desired output:

YEAR;MONTH;DAY;HAD_ECOMAG;HAD_HYPE
1971;01;01;4138.3;4390
1971;01;02;4129;4380
1971;01;03;4115.9;4370
3

So you want to:

  • split the filename into underscore delimited fields
  • prepend the second field of the filename to the fourth and greater header fields, in upper case

In awk:

awk '
  BEGIN{FS=";"; OFS=FS} 
  FNR==1 {
    split(FILENAME,a,"_"); 
    for (i=4; i<=NF; i++) $i = sprintf("%s_%s", toupper(a[2]), $i)}1
' *85.csv

e.g.

$ awk 'BEGIN{FS=";"; OFS=FS} FNR==1 {split(FILENAME,a,"_"); for (i=4; i<=NF; i++) $i = sprintf("%s_%s", toupper(a[2]), $i)}1' *85.csv
YEAR;MONTH;DAY;GFDL_WFDisi;GFDL_SWIM;GFDL_GAP3;GFDL_HYPE;GFDL_VIC
1971;01;01;1206.1;627;1252.6;909;357.6
1971;01;02;1303.4;654.3;1397.4;916;478.5
1971;01;03;1248.9;662;1318.8;951;653.1
YEAR;MONTH;DAY;HAD_ECOMAG;HAD_HYPE
1971;01;01;4138.3;4390
1971;01;02;4129;4380
1971;01;03;4115.9;4370

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.