I wrote a simple function in bash to search the first line of a CSV file and return the column number of a matching header value:
findcolnum()
{
colnum=`head -1 $1 | awk -F"," '{for(i=1;i<NF;i++) if($i==$2) {print i}}'`
echo $colnum
}
When I make the call to the function:
analogcol=`findcolnum "$DIR/PCICCP_AnalogPoint_DMS.csv" "AnalogPointRef"`
The result is always "2" no matter whether the column exists or not.
While on command line, where I tested the command before putting it into a script, it is returning the correct value:
head -1 Outbox/ICCP/PCICCP_AnalogPoint_DMS.csv | awk -F"," '{for(i=1;i<NF;i++) if($i=="AnalogPointRef") {print i}}'
9
Can anyone help me understand what's going on here?