1833606 Members
3249 Online
110062 Solutions
New Discussion

awk help

 
Jose Mosquera
Honored Contributor

awk help

I have the following trouble with awk command inside of a script file, I'm trying to insert $COUNTRY variable inside of print statement. $COUNTRY always be a numeric value.

cat access.idx|grep ^"2.$COUNTRY."|awk -F"." '{ print $3 }'|sort|uniq -c|awk '{ print "2."$COUNTRY"."$2".*\t"$1 }'

the following error output is displayed:
awk: Field $() is not correct.
The input line number is 1.
The source line number is 1.

I'm trying with "awk -v" argument, but I can not achieve a successfully result.
5 REPLIES 5
Jose Mosquera
Honored Contributor

Re: awk help

Hi Pals,

I have resolved this by:
cat access.idx|grep ^"2.$COUNTRY."|awk -F"." '{ print $3 }'|sort|uniq -c|awk -v CO=$COUNTRY '{ print "2."CO"."$2".*\t"$1 }'

Thanks anyway
H.Merijn Brand (procura
Honored Contributor

Re: awk help

Something like this?

# perl -ne's/^2\.$ENV{COUNTRY}\.// and print' access.idx | sort -u | sed "s/^/2.$COUNTRY/'

Enjoy, Have FUN! H.Merijn
Tom Danzig
Honored Contributor

Re: awk help

You could do:

cat access.idx|grep ^"2.$COUNTRY."|awk -F"." '{ print $3 }'|sort|uniq -c|awk x=$COUNTRY '{print "2."x"."$2".*\t"$1 }'
H.Merijn Brand (procura
Honored Contributor

Re: awk help

Your problem was in the quote style you use:

awk '{ print "2."$COUNTRY"."$2".*\t"$1 }'

uses single quotes around the scriplet, causing $COUNTRY not to be evaluated as value, but as literal '$COUNTRY', something very unlikely to appear in your access.idx :)

awk '{ print "2."'$COUNTRY'"."$2".*\t"$1 }'

would have done the job to (see the single quotes around $COUNTRY)
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: awk help

Change your last awk statement to this:


'{hello=system("echo $COUNTRY"); printf("2. %s.%s.%s",hello,$2,$1); }'


live free or die
harry
Live Free or Die