I'm trying to extract a row/column element from a text table using awk within a function in bash. Specifically:
getel() {
awk -v col=$3 'FNR==$2 {print $col }' $1
}
The usage is:
getel <file> <row> <col>
If I type in:
awk -v col=2 'FNR==1 {print $col}' infile
at the bash prompt, I get row 1 col 2 as expected.
I dug through the various bash-awk questions but none seemed to get me to a solution.
Ultimately, I want to assign the result to another bash variable, ie:
FOUND=`awk -v col=2 'FNR==1 {print $col}' infile`
thanks
Pass another command line variable to awk:
getel() {
awk -v row=$2 -v col=$3 'NR==row {print $col }' $1
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With