For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2007 > Assigning Variables on the Command Line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Assigning Variables on the Command Line
Hermann Peifer

2007-03-04, 6:57 pm

Hi All,

I have this "semi-interactive" GAWK script, where I can specify 3
variables on the command line:

$ awk -v component=7 -v type=1 -v coverage=99 -f my_script.awk

The script looks like this:

BEGIN {
FS = OFS = "\t"
dt[1]="day"
dt[2]="dymax"
dt[3]="hour"
}

$2 == component && $5 == dt[type] && $10 >= coverage {
# some record processing logic here
}
END{
# some END logic here
}

To specify ANY coverage is easy:
$ awk -v component=7 -v type=1 -v coverage= -f my_script.awk

But how would I specify ANY component and ANY type?

Perhaps with some sort of: dt[0]=[some RE], component[0]=[:digit:] ?

Hermann
Hermann Peifer

2007-03-04, 6:57 pm

Hermann Peifer wrote:
> Hi All,
>
> I have this "semi-interactive" GAWK script, where I can specify 3
> variables on the command line:
>
> $ awk -v component=7 -v type=1 -v coverage=99 -f my_script.awk
>
> The script looks like this:
>
> BEGIN {
> FS = OFS = "\t"
> dt[1]="day"
> dt[2]="dymax"
> dt[3]="hour"
> }
>
> $2 == component && $5 == dt[type] && $10 >= coverage {
> # some record processing logic here
> }
> END{
> # some END logic here
> }
>
> To specify ANY coverage is easy:
> $ awk -v component=7 -v type=1 -v coverage= -f my_script.awk
>
> But how would I specify ANY component and ANY type?
>
> Perhaps with some sort of: dt[0]=[some RE], component[0]=[:digit:] ?


Sorry: component can't be a scalar and an array at the same time. But I
guess you understand what I mean.

Hermann
Jürgen Kahrs

2007-03-04, 6:57 pm

Hermann Peifer wrote:

> To specify ANY coverage is easy:
> $ awk -v component=7 -v type=1 -v coverage= -f my_script.awk
>
> But how would I specify ANY component and ANY type?
>
> Perhaps with some sort of: dt[0]=[some RE], component[0]=[:digit:] ?


No, I dont think this will work.
In cases like these, I usually define a special
value (for example component = -1) to have a
special meaning (ANY). This special value has to be
outside the normal range of legal values. Then I
change the script so that the comparison

$2 == component

is replaced by something like

(component == -1 || $2 == component)
Hermann Peifer

2007-03-04, 6:57 pm

Jürgen Kahrs wrote:
> Hermann Peifer wrote:
>
>
> No, I dont think this will work.


It doesn't. At least not with my limited knowledge.

> $2 == component
>
> is replaced by something like
>
> (component == -1 || $2 == component)


Simple and smart. That's what I was looking for. I am still thinking in
too complicated patterns. Just not awkish enough.

Hermann
Ed Morton

2007-03-16, 8:00 am

Hermann Peifer wrote:
> Hi All,
>
> I have this "semi-interactive" GAWK script, where I can specify 3
> variables on the command line:
>
> $ awk -v component=7 -v type=1 -v coverage=99 -f my_script.awk
>
> The script looks like this:
>
> BEGIN {
> FS = OFS = "\t"
> dt[1]="day"
> dt[2]="dymax"
> dt[3]="hour"
> }
>
> $2 == component && $5 == dt[type] && $10 >= coverage {
> # some record processing logic here
> }
> END{
> # some END logic here
> }
>
> To specify ANY coverage is easy:
> $ awk -v component=7 -v type=1 -v coverage= -f my_script.awk
>
> But how would I specify ANY component and ANY type?
>
> Perhaps with some sort of: dt[0]=[some RE], component[0]=[:digit:] ?
>
> Hermann


The problem you're having is that you want to specify an RE on the
command line but you aren't testing for an RE in your script (using "~"
instead of "=="). If you just change your script to:

BEGIN {
FS = OFS = "\t"
dt[0]="*"
dt[1]="day"
dt[2]="dymax"
dt[3]="hour"
}

$2 ~ ("^" component "$") && $5 ~ ("^" dt[type] "$") && $10 >= coverage {
# some record processing logic here
}

then you could pass in an RE for component and a zero value for type:

awk -v component="[[:digit:]]" -v type=0 -v coverage= -f my_script.awk

to get what you want.

Ed.
Hermann Peifer

2007-03-16, 9:57 pm

Ed Morton wrote:
> Hermann Peifer wrote:
>
> The problem you're having is that you want to specify an RE on the
> command line but you aren't testing for an RE in your script (using "~"
> instead of "=="). If you just change your script to:
>
> BEGIN {
> FS = OFS = "\t"
> dt[0]="*"
> dt[1]="day"
> dt[2]="dymax"
> dt[3]="hour"
> }
>
> $2 ~ ("^" component "$") && $5 ~ ("^" dt[type] "$") && $10 >= coverage {
> # some record processing logic here
> }
>
> then you could pass in an RE for component and a zero value for type:
>
> awk -v component="[[:digit:]]" -v type=0 -v coverage= -f my_script.awk
>
> to get what you want.
>
> Ed.


Thanks for this solution. I will give it a try on Monday. Could it be
that this line:

> dt[0]="*"


should read:

> dt[0]=".*"


?

Hermann
Ed Morton

2007-03-16, 9:57 pm

Hermann Peifer wrote:

> Ed Morton wrote:
>
>
>
> Thanks for this solution. I will give it a try on Monday. Could it be
> that this line:
>
>
> should read:
>
>


Yes.

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com