Home > database >  awk to print the colums with condition on header
awk to print the colums with condition on header

Time:09-28

I have this kind of input:

Vel RMSE H D20120122 D20130404 X Y
0.5 0.1  3 4         5         2 2

I would like to print only the colums containing the string D2, in order to have as output the following:

D20120122 D20130404
4         5

I tried like this: awk '{for(i=1;i<=NF;i ) if($i ~ /D2/) printf $i}' myfile

but it returns only the first row, i.e.:

D20120122 D20130404

Any idea?

Thanks

CodePudding user response:

Using lengthy variable names for clarity:

$ cat tst.awk
NR==1 {
    for ( inFldNr=1; inFldNr<=NF; inFldNr   ) {
        if ( $inFldNr ~ /D2/ ) {
            out2in[  numOutFlds] = inFldNr
        }
    }
}
{
    for ( outFldNr=1; outFldNr<=numOutFlds; outFldNr   ) {
        inFldNr = out2in[outFldNr]
        printf "%s%s", $inFldNr, (outFldNr<numOutFlds ? OFS : ORS)
    }
}

$ awk -f tst.awk file
D20120122 D20130404
4 5

$ awk -f tst.awk file | column -t
D20120122  D20130404
4          5

CodePudding user response:

One option might be keeping track of the column numbers that match the pattern D2 in the first row.

Then for the other rows, only print the matching field numbers.

awk '
BEGIN { ORS="" };
{
  if (NR==1) {
    for(i=1; i<=NF; i  ) {
      if($i ~ /D2/) {
        arr[  keyNr] = i
      }
    }
  }
  for (y=1; y<=keyNr; y  ) {
    printf("%-10s", $arr[y])
  }
  print "\n"
}' myfile

Output

D20120122 D20130404 
4         5   

AWK demo

CodePudding user response:

 awk 'NR==1{for(i=1; i<=NF; i  ) {if($i ~ /D2/) a[i] = 1}} NR > 1{for(i in a) print $i}' input
  • Related