help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: joining records depending on special conditions - a job for gawk?


From: olli
Subject: Re: joining records depending on special conditions - a job for gawk?
Date: 22 Aug 2005 00:14:30 -0700
User-agent: G2/0.2

David Resnick wrote:

[...]

>
> How about something like this?  Or did you want gawk instead
> of perl for some reason?

I'm not (and assume will not be) a programmer but try hard to solve
problems with scripts if it's necessary and I'm interested in Perl
because of its capacity (especially ability to connect to MySQL) and
"elegance".

First of all I try to get quick ;-) and simple solutions with the tools
I already know better than Perl (in this case gawk)

[...]


Here are my 2 little gawk-scripts that bring me ahead:

1. get a count per line

BEGIN { FS = OFS = "\t" }

      { cnt = 0
              for ( i = 3; i <= NF; i++ )
                      if ( $i == 214 || $i == 220 || $i == 223 || $i == 214 
||228 $i
== 240 || $i == 252 ) {
                              cnt++
                         }
        print $1, $2, cnt
      }


(BTW: I think your PERL-script disregards that a record can have a
varying number of fields)


2. sum up for the groups


BEGIN   { FS = OFS = "\t" }
        { cntansi[$1"\t"$2] += $3 }
END     { for (grosso in cntansi)
                print grosso, cntansi[grosso]
        }

Thanks for your idea and

greetings
Oliver Peters



reply via email to

[Prev in Thread] Current Thread [Next in Thread]