I want to do something of the sort:
for i in 1 2 3
do
gawk '{if ($i==$2) {print $0;}}' filename
done
is this possible?
thanks
I want to do something of the sort:
for i in 1 2 3
do
gawk '{if ($i==$2) {print $0;}}' filename
done
is this possible?
thanks
Well sure, it's possible, but it probably won't do what you want it to do.
...come to think of it, what do you want it to do? It's not really clear from what you've written. One thing you'll probably have to change is to replace the single quotes with double quotes because variables (like $i
) aren't substituted into single-quoted strings.
Assuming that you want the gawk script to be
{if ($1 == $2 ) ... } {if ($2 == $2 ) ... } {if ($3 == $2 ) ... }
you can do:
for i in 1 2 3 do gawk '{if ($'$i'==$2) {print $0;}}' filename done
Two possible interpretations of your script come to mind:
filename
Here are both versions of the loop:
for i in 1 2 3; do
gawk "{if (\"$i\"==\$2) {print \$0;}}" filename
done
for i in "$@"; do
gawk "{if (\"$i\"==\$2) {print \$0;}}" filename
done
In order to avoid ugly quoting, you can use gawk's variable passing feature:
for i in 1 2 3
do
gawk -v param=$i '{if (param==$2) {print $0}}' filename
done