-1

I have a large file called an.txt:

head -n 6 an.txt 

Type    Details Particulars Code    Reference   Amount  Date    ForeignCurrencyAmount   ConversionCharge
Bank Fee    Monthly A/C Fee             -8.50   31/03/2021      
Eft-Pos Rutherford & Bond   4835********    8848   C    210331123119    -250.00 31/03/2021      
Payment Avery Johnson   Avery Johnso    592315  Labour  -131.60 31/03/2021      
Bill Payment    Collins Tf  127 Driver  Crescent    I1600   50.00   31/03/2021      
Bill Payment    Becta Ltd   Taylormallon    Lawns   Inv 1447    46.00   31/03/2021
.
.
.

I have written the following script called b1.awk:

#! /usr/bin/awk -f

BEGIN{
    FS = "\t"
    }
$2 ~ /Lance Te/ {
    s+=$6
    }
END{
    print "Lance Te Patu: " s
    } 

BEGIN{
        FS = "\t"
        }
$2 ~ /Matti/ {
        s+=$6
        }
END{
        print "Mattingly Court: " s
        }

When called ./b1.awk an.txt I get:

Lance Te Patu: 3170.17
Mattingly Court: 3170.17

The first thing here is that the results are incorrect. The first one is correct but the second should be different. I am not sure why this does not work. The second question is whether /Lance Te/ and /Matti/ can be passed as variables rather than writing seperate awk statements which is what I ideally would like to achieve. I am sorry but I am trying to wrap my head around awk in case this seems a bit dumb to some of you.

Christian Hick
  • 401
  • 3
  • 10
  • Your question should contain a single question. It should not be hard to find a duplicate for how to assign a regex as a variable (basically `awk -v var="Matti" '$2 ~ var'`) so maybe just take out that part. But more fundamentally, you really need to [edit] to explain why the results are wrong and what to expect instead and why; code which doesn't do what you want is a terrible way to explain what you do want. Also, please show us your debugging efforts. – tripleee Dec 30 '21 at 08:15

2 Answers2

1

You are requiring a fair bit of mind reading, but my crystal ball suggests you might be looking for something like

awk -F '\t' -v regex="Matti|Lance Te" '$2 ~ regex { sum[$2] += $6 }
    END { for (rcpt in sum) print rcpt ": " sum[rcpt] }' an.txt

where I have assumed that you want to extract the full text from $2 as the actual recipient, although the regex looks for just a substring in each case.

In some more detail, we collect the interesting totals into an associative array sum where the key for each value is the string from $2 and the value is the accrued total of values for $6 from the rows where the key was found.

The immediate problem with your attempt was that you used the same variable s for each sum; you could trivially fix it by using t instead of s in the second set of statements. But repeating the code for each search string is obviously inelegant, cumbersome, and error-prone.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • We are not really supposed to engage in mind reading here; please review the [help] and in particular [How to ask](/help/how-to-ask) as well as the guidance for providing a [mre]. Notice how your sample data does not contain any matches for either of the strings you are looking for; how are we supposed to decide whether the results we get are correct? – tripleee Dec 30 '21 at 08:21
0
#!/usr/bin/awk -f

BEGIN{
    FS = "\t"
}

$2 ~ /Lance Te|Matti/ {
    s[$2] += $6
}

END {
    for (i in s)
        print i ": " s[i]
}
konsolebox
  • 72,135
  • 12
  • 99
  • 105