On 3/12/2008 12:38 PM, tbh wrote:
> hi, i'm not a complete awk beginnger, but also no expert.
>
> I'm trying to do something reasonably simple and am having trouble. i
can do
> the same thing in perl, but my case (reading pretty massive input under
> cygwin under windows) that degrades performance.
>
> I'd like to use a variable as part of a regular expression as follows:
> $ unpackHourlyWebLogs.bash 11 | gawk '(tolower($5) ~
("^/"+base+"/"))
> {print $3,$5,$6}' base="f30" | head
unpackHourlyWebLogs.bash 11 | gawk -v base="f30" 'tolower($5) ~ "^/" base
"/"
{print $3,$5,$6} NR==10{exit}'
Someone had suggested you do something like this:
base="f30"
unpackHourlyWebLogs.bash 11 | gawk 'tolower($5) ~ "^/'"$base"'/" {print
$3,$5,$6} NR==10{exit}'
to use a shell variable in the middle of your awk script. Don't do that as
it
can lead to hard-to-diagnose failures, e.g.:
$ base="a"
$ printf "/a/\n/b/\n" | gawk -v base="$base" '$0 ~ "/"base"/"'
/a/
$ printf "/a/\n/b/\n" | gawk '$0 ~ "/'"$base"'/"'
/a/
$ base="
> "
$ printf "/a/\n/b/\n" | gawk -v base="$base" '$0 ~ "/"base"/"'
$ printf "/a/\n/b/\n" | gawk '$0 ~ "/'"$base"'/"'
gawk: $0 ~ "/
gawk: ^ unterminated string
Regards,
Ed.


|