Suppose I have a CSV file with the headers of the following form:
Field1,Field2 3,262000 4,449000 5,650000 6,853000 7,1061000 8,1263000 9,1473000 10,1683000 11,1893000
I would like to write an awk script that takes a list of comma delimited target field names, splits it into an array, and selects only those columns with the names that I specify.
This is what I have tried so far, and I have made sure that the head array contains the desired headers, and the targets array contains the targets passed by this command line.
BEGIN{ FS="," split(target, targets, ",") } NR==1 { for (i = 1; i <= NF; i++) head[i] = $i } NR !=1{ for (i = 1; i <= NF; i++) { if (head[i] in targets){ print $i } } }
When I invoke this script with the command
awk -v target = Field1 -f GetCol.awk Debug.csv
I am not printing anything.
source share