user3158991
user3158991

Reputation: 21

Awk--For loop by comparing two files

I have two big files.

File 1 looks like following:

10 2864001 2864012
10 5942987 5943316

File 2 looks like following:

10 2864000 28
10 2864001 28
10 2864002 28
10 2864003 27
10 2864004 28
10 2864005 26
10 2864006 26
10 2864007 26
10 2864008 26
10 2864009 26
10 2864010 26
10 2864011 26
10 2864012 26

So I want to create a for loop in such a way that,

  1. First column of File 1 must match first column of File 2 AND
  2. To start a for loop by matching second column of File 1 with second column of File 2 AND
  3. Sum third column of File 2 until third column of File 1 match to second column of File 2.

So the output of above example should be sum of third column of File 2 for first line of File 1 which is 347. I tried to use NR and FNR but I have not been able to do it so far. Could you please help me to generate awk script?

Thank you so much

Upvotes: 0

Views: 1051

Answers (2)

Håkon Hægland
Håkon Hægland

Reputation: 40778

You could try

awk -f s.awk file1 file2

where s.awk is

NR==FNR {
    a[$1,$2]=$3
    next
}
($1,$2) in a {
    do {
        s+=$3
        if ((getline)+0 < 1) break
    } while ($2 != a[$1,$2])
    print s
}
{ s=0 }

output:

319

Upvotes: 0

mpez0
mpez0

Reputation: 2883

Transcribed, so there may be typos:

awk '
BEGIN { lastFNR=0; acount=0; FIRST="T"}

FNR < lastFNR {FIRST="F"; aindex=0; next}

FIRST=="T" {
      sta[acount] = $2
      fna[acount] = $3
      acount += 1
      lastFNR=FNR         
}

FIRST=="F" && $2 >= sta[index] && $2 <= fna[aindex] {
      sum[aindex] += $3
      lastFNR = FNR
}

FIRST=="F" && $2 > fna[aindex] {
      aindex ==1
      if (aindex > acount) { FIRST="E" }
}

END {
      for(aindex=0; aindex<acount; +=aindex) {
            print sta[aindex], "through", fna[index], "totals", sum[aindex]
      }
}
' file 1 file2

Upvotes: 0

Related Questions