1827452 Members
4105 Online
109965 Solutions
New Discussion

awk search wildcards

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

awk search wildcards

hi,
I've got an awk script thanks to help on the itrc that I'm still playing around with.
It's called as follows from within a script.

awk -f script.awk search=$2 filename

The first line of the awk script looks as follows.

$0 ~ "" search{GOTIT=1}

Now, I've discovered that I can have wildcards passed from the shell script command line as follows:

$2 = 20..
will report 2001 2002 2003 -- 2099 etc..
$2 = 20. will report 201 - 209
$2 = 20 will report 1020 1120 etc..

how can I specify that the $2 of 2 only lists 2 and not 2222 2000 200 192 92 etc...



Thanks,
Bill
It works for me (tm)
3 REPLIES 3
Curtis Larson_1
Valued Contributor
Solution

Re: awk search wildcards

you'll need to do something like this:

$2="^2 | 2$|( 2 )|^2$"

basically, match a 2 with a blank space before and after, at the beginning of a line, at the end of a line, or on a line by itself.

if your pattern, 2 in this case, is delimited by something other then a space or in addition to a space, maybe a tab, you'll need to modify as appropriate.
James R. Ferguson
Acclaimed Contributor

Re: awk search wildcards

Hi Bill:

Pass a regular expression as:

# awk -f script.awk search=[^$2]$2[^$2] filename

In this case, only isolated character occurances equal to the character defined as $2, following the string "", will be printed from your script.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: awk search wildcards

Hi (again) Bill:

Ten-points to Curtis! His solution handles the case where the matching single character occurs at the line's end (mine doesn't). Curtis' solution generalizes for the case where you want to match a single character at the beginning of a line too (mine doesn't).

Regards!

...JRF...