0

I want to extract some columns by their names listed on a different file.

I found this useful code as in the following link: AWK extract columns from file based on header selected from 2nd file

And I tried to edit some to print header and limit the length of header.

#!/bin/bash

DATAFILE=${1:-data.txt}
COLUMNFILE=${2:-list.txt}

awk -v colsFile="$COLUMNFILE" '
   BEGIN {
     j=1
     while ((getline < colsFile) > 0) {
        col[j++] = $1
     }
     n=j-1;
     close(colsFile)
     for (i=1; i<=n; i++) s[col[i]]=i
   }
   NR==1 {
     for (f=1; f<=NF; f++)
       if ($f in s) c[s[$f]]=f
       printf ${$f:0:3}
   }
   { sep=""
     for (f=1; f<=n; f++) {
       printf("%c%s",sep,$c[f])
       sep=" "
     }
     print ""
   }
' "$DATAFILE"

I used the same example in the link before.

$ cat data.txt
ID,head1,head2,head3,head4
1,25.5,1364.0,22.5,13.2
2,10.1,215.56,1.15,22.2

$ cat list.txt
ID
head1
head4

$ dataExtractor.sh data.txt list.txt
1,25.5,13.2
2,10.1,22.2

And Output I want is

ID,hea,hea
1,25.5,13.2
2,10.1,22.2

After I edit the code to include ${$f:0:3}, it gives me a syntax error. Please help me with this, thanks!

Community
  • 1
  • 1

1 Answers1

0

From man 1 awk:

   substr(s, i [, n])      Return  the  at most n-character substring of s
                           starting at i.  If n is omitted, use  the  rest
                           of s.

...

substr($f, 0, 3)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358