## Awk question - need to pass a variable.

### Awk question - need to pass a variable.

I am posting the three solutions to my question.
I would like to thank following people for their contribution:

#! /bin/sh
#Solution 1: The QUOTES are very important.

set `date`; mo=\$2; day=\$3
echo "Today is \$mo \$day"
awk -F: '\$2 == "'\$mo'" { print \$2, \$3, \$1 }' bdays
#              =======

#Solution 2: Here since we are searching for \$2 in awk
#            in the entire input line, there is a chance
#            that name Maria may also be echoed unnecessarily
#            during March even if her b'day is in say December.

#! /bin/sh
set `date`; mo=\$2; day=\$3
echo "Today is \$mo \$day"
awk -F: '/'\$2'/ { print a, \$3, \$1 }' a=\$2 bdays

#Solution 3: Alternative for Solution 2
#Data file format should be Month:Day:Name in this case

#! /bin/sh
set `date`; mo=\$2; day=\$3
echo "Today is \$mo \$day"
awk -F: '/'^\$2'/ { print \$1, \$2, \$3 }' a=\$2 bdays

Note: Please note defining variable 'a' on command line of awk.
It is used in print statement(in solution 2).

### Awk question - need to pass a variable.

I am posting the three solutions to my question. (Small correction)
I would like to thank following people for their contribution:

#! /bin/sh
#Solution 1: The QUOTES are very important.

set `date`; mo=\$2; day=\$3
echo "Today is \$mo \$day"
awk -F: '\$2 == "'\$mo'" { print \$2, \$3, \$1 }' bdays
#              =======

#Solution 2: Here since we are searching for \$2 in awk
#            in the entire input line, there is a chance
#            that name Maria may also be echoed unnecessarily
#            during March even if her b'day is in say December.

#! /bin/sh
set `date`; mo=\$2; day=\$3
echo "Today is \$mo \$day"
awk -F: '/'\$2'/ { print a, \$3, \$1 }' a=\$mo bdays

#Solution 3: Alternative for Solution 2
#Data file format should be Month:Day:Name in this case

#! /bin/sh
set `date`; mo=\$2; day=\$3
echo "Today is \$mo \$day"
awk -F: '/'^\$2'/ { print \$1, \$2, \$3 }' a=\$2 bdays

Note: Please note defining variable 'a' on command line of awk.
It is used in print statement(in solution 2).

I was wondering if there was any other way to pass variables as regular
expressions in an awk script. I have the script 'shellreport' as follows:

#!/bin/sh
shell=\$1
datafile=\$2

awk -F: '
# \$NF is the last field of /etc/passwd containing shell info
\$NF ~ /'"\$shell"'/ { print \$0
++count }
END { print count " entries found for shell '"\$shell"' "

which is invoked like
\$ shellreport bash /etc/passwd

This is fine. What I was wondering was if there is any way not using
shell variables \$1 and \$2 etc that I could do the same thing entirely
in awk. I would like to be able to just say:

\$ awk -f countshell shell=bash /etc/passwd

where countshell was something like this:

BEGIN { FS=":" }
# \$NF is the last field of /etc/passwd containing shell info
\$NF ~ /shell/ { print \$0
++count }
END { print count " Entries found for " shell }

The problem is using 'shell' in the regular expression.
I have documentation explaining this in awk but I am having a bit
of trouble understanding it.

-tms

--
From the Lab of the MaD ScIenTiST: