I hope that I am not beating dead horse with this.

There is also performance problem with using case/switch statement like
this - the whole code block gets evaluated/run for every record/line.

If you use it the way suggested, the code block is only run for the correct
record/lines. You can optimize it further if you keep simple comparison
such as NF==35 condition before regexp comparison/search.

Depending on your awk script and/or your data - this can have significant
runtime impact, beside nicer coding style.

Tomas

On Mon, Jul 23, 2018, 3:21 PM Rich Shepard <[email protected]> wrote:

> On Mon, 23 Jul 2018, Tomas Kuchta wrote:
>
> > Do not use switch/case - just use NF==35 {print "I see 35 columns on this
> > line"}
> > ... type of a code.
> >
> > If you need more than that you can do something like this:
> > NF==35 && $2<5 {print "I see 35 columns on this line and column 2 is less
> > than 5"}
> >
> > I guess that is what Russell was saying too.
>
> Tomas,
>
>    It turns out that the switch/case statement works when the whole thing
> is
> enclosed in curly braces because it's all part of the action response. So
> it
> would look like this:
>
> { switch (NF) {
>    case 1:
>      ...
>    case 2:
>      ...
>    }
> }
>
>    But, using the number of fields as the pattern does make it easier to
> read:
>
> NF == 36 { print .... }
>
> Thanks,
>
> Rich
> _______________________________________________
> PLUG mailing list
> [email protected]
> http://lists.pdxlinux.org/mailman/listinfo/plug
>
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to