Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to use a shell variable in search criteria

Status
Not open for further replies.

cp2000

Technical User
Dec 31, 2002
81
US
I looked at my gawk man page
I read thru the FAQ on passing shell variables

But neither seemed to address my need. I need to pass a shell variable to the search criteria in my gawk command

commandline:
-----------
script aol
-----------

script
-----------
#!/bin/bash
DOMAIN=$1
mailq \
|gawk -v v1=$DOMAIN 'BEGIN{RS=""} /$v1/ { print $1 }'
-----------

mailq gives me the list of emails currently queued. I seem to be able to get the variable into gawk. Just nop into the search criteria.

 
Either use this:
mailq \
|gawk -v v1=$DOMAIN 'BEGIN{RS=""} $0~v1 { print $1 }'
Or this:
mailq \
|gawk 'BEGIN{RS=""} /'"$DOMAIN"'/ { print $1 }'


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
In awk, gawk, mawk, the built-in array ENVIRON conains the values of environmental variables.

[tt]
#!/bin/bash
DOMAIN=$1
mailq \
|gawk 'BEGIN{RS=""} $0 ~ ENVIRON["DOMAIN"] { print $1 }'
[/tt]
 
futurelet, you have to export the variable to put them in the environ.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top