Home > Software engineering >  Using shell variable to select awk column identifier
Using shell variable to select awk column identifier

Time:02-07

This one works:

temp=($(awk -F" "  '$2 == 84' "$1".db))

This one does not (fieldnumber stores the column i wish to search, and val stores the value I'm searching for):

temp=($(awk -F" " -v column="$""$field_number" -v val="$3" '{ if(column == val) print $0;}' "$1".db))

I am trying to manipulate the awk command based on command line inputs within shell script: ./dr.sh cop4342 exam1 84 (84 is the value to be searched for) (exam1 is used within the shell script to find the column number)

code image code output

CodePudding user response:

This is terribly unclear, but I guess you are looking for

temp=($(awk -F " " -v column="$field_number" \
            -v val="$3" '{ if($column == val) print $0;}' "$1".db))

which of course can be simplified to just

temp=($(awk -v column="$field_number" \
            -v val="$3" '$column == val' "$1".db))
  •  Tags:  
  • Related