Peter Rundle wrote:
Sluggers,
Does anyone know how to use an argument to awk as a pattern match?
I.E
#!/bin/bash
#
SEARCH="SomeString"
awk --assign search=$SEARCH '
BEGIN { printf("Search:%s\n",search) }
/search/ { printf("%s\n",$0) }'
The printf statement produces "SomeString" as expected but not
surprisingly the '/search/' pattern matches "search". How can I make awk
understand that I want to use the value of the variable 'search' not the
literal string?
TIA's
P.
P.S Yes perl heads feel free to use the opportunity to show the mastery
of perl over awk ;-)
Okee dokee ... here is grep.pl:
--------------------------------------
#!/usr/bin/env perl
$regex = qr/\Q$ARGV[0]/;
while (<STDIN>) { print $_ if /$regex/ }
--------------------------------------
Note: you can make the search case insensitive as follows:
$regex = qr/\Q$ARGV[0]/i; # <--- tack on the 'i' option
--
_________________________________
Rick Welykochy || Praxis Services
Few things are harder to put up with than the annoyance of a good example.
--Mark Twain
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html