gawk

awk switch case with constant variable


I have some problems with gawk an the switch case statement. When I use switch case with an constant string every thing works fine, but wenn I use a constant variable it doesn't.

For better explanation two examples.

This example works fine:

BEGIN { 
 ...
}
END {
    split($0,a,", ") 

    for (k in a)
    {
     switch (a[k]) 
     {
      case "COLUMN 1":
       POSITION = k
       print k,a[k]
       break
      default:
       print "Error"
       exit
       break
    }
}

This example gives me a Syntax Error:

BEGIN { 
    COLUMN_NAME = "COLUMN 1"
}
END {
    split($0,a,", ") 

    for (k in a)
    {
     switch (a[k]) 
     {
      case COLUMN_NAME :  #Syntax Error in this line
       POSITION = k
       print k,a[k]
       break
      default:
       print "Error"
       exit
       break
    }
}

I don't know if awk makes COLUMN_NAME an constant, but I did not find any way to force this. I even try to use an if/else this works fine in both cases.

Edit: Here is an explaination what the awk script should do. I have a CSV file looked like this:

COLUMN 1, COLUMN 2, COLUMN 3, COLUMN 4
       1,        2,        3,        4
       5,        6,        7,        8
...

but the file can even look like this:

COLUMN 3, COLUMN 2, COLUMN 4, COLUMN 1
       1,        2,        3,        4
       5,        6,        7,        8
...

I know the name of the column's, but I didn't know the positon. So I parse the column names with the split function and would use an switch to find the right position.


Solution

  • Here is a way to sort it out using array in awk

    awk -F, 'NR==1 {for (i=1;i<=NF;i++) {split($i,t," ");c[i]=t[2]}} NR>1 {for (j=1;j<i;j++) arr[(NR-1)FS c[j]]=$j+0} END {print arr[2 FS 1]}' file
    

    Then END prints second row, column 1
    This will for first file give 5
    and 8 for second file


    Some more readable:

    awk -F, '
    NR==1 {                             # get the column order
        for (i=1;i<=NF;i++) {           # loop trough all fields
            split($i,tmp," ")           # get the column number
            col[i]=tmp[2]}              # store the column order in col
        } 
    NR>1 {                              # for all data do:
        for (j=1;j<i;j++)               # loop trough all element
            arr[(NR-1)FS col[j]]=$j+0}  # store data in array arr
    END {
        print arr[2 FS 1]}              # print data from row 2 column 1
    ' file