I am trying to pass a variable number of arguments from the shell script to a pattern recognition subset of a table. Here is my attempt so far:
The file 'infile':
ID,GROUP
1,GROUP2
2,GROUP2
3,GROUP4
4,GROUP4
5,GROUP5
6,GROUP5
7,GROUP23
8,GROUP23
9,GROUP23
The file subset.sh:
#!/bin/sh
rm -f outfile_$week
week = $1
shift
for TOKEN in "$@"
do
echo "adding records for" $TOKEN
awk -F "," -v group = $TOKEN '{ if(FNR > 2 && $2 ~/group/){print $0} }' infile >> outfile_$week
done
I have also tried group = "$TOKEN", "group = $TOKEN" and then both with single quotes. I am submitting like this:
sh subset.sh 061314 GROUP2 GROUP23
The error I get is an astoundingly uninformative
Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|File] ...
Any help is much appreciated, thanks!
EDIT: I tried running
awk -F "," -v group ="GROUP1" '{ if(FNR > 2 && $2 ~/group/){print $0} }' infile
to no avail... (same error as above) anyone know of any reason this might happen?
rm -f infile
doing insidesubset.sh
? – iruvar Jun 13 '14 at 18:24$2 ~/group/
will do a regex match against literal stringgroup
. To match against the variable you need$2 ~ group
. In fact the whole expression could be much simpler'FNR > 2 && $2 ~ group'
sinceprint $0
is the default action if the test evaluatestrue
. – steeldriver Jun 13 '14 at 18:38=
. That's what is wrong with everything you have tried. Gnouc's answer is the correct way. – Graeme Jun 13 '14 at 19:37