Awk question - need to pass a variable.

Awk question - need to pass a variable.

Post by NL » Fri, 12 Mar 1993 04:53:48



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.

Post by NL » Fri, 12 Mar 1993 05:41:34


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).

 
 
 

1. Question about passing variable as reg exp in awk

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:


2. example XF86Config file for Matrox G200 PCI?

3. Passing variables to awk in ksh

4. Memory dilemma - Help

5. passing awk variables as arguments

6. who is eating inodes ?

7. pass variables to awk

8. [HELP} programable Clock chips for S3 805

9. awk - passing in a variable expression

10. Passing shell variables to awk?

11. search on variable passed to awk /nawk

12. Passing shell variable to awk that contains spaces

13. Passing a variable from awk to shell