-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfield_sum.awk
67 lines (62 loc) · 1.44 KB
/
field_sum.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/awk -f
#
# script to sum up the values of a selected field/column. string values will be ignored
#
# a value for variable "fieldnumber" must be passed to this script. it defines which
# field should be used as the dimension field/column
#
# if you specify variable "noheader" as "1", you indicate that the file has NO header
# row
#
# if you specify "quiet" as "1", you indicated that no output should be generated.
#
# example call of this script:
#
# field_sum.awk -v fieldnumber=7 -v noheader=1 /home/testuser/testfile.csv
#
#
# http://datamelt.com
#
# last update: 2010-03-24
#
#
# begin of processing
BEGIN {
# setting the file's field seperator
FS=";";
counter = 0;
sum=0;
fieldname=fieldnumber;
}
# first line is the header row. we retrieve the name of the selected field.
NR <= 1 && !noheader {
fieldname = $fieldnumber;
}
# we skip the first header row and only take those rows matching with the query parameter
(NR > 1 || noheader==1) {
# in case the does not contain a value, we ignore it
if($fieldnumber!="")
{
sum = sum + $fieldnumber;
}
}
END {
if(quiet!=1)
{
print "";
# give out which field we are using
print "evaluated field: " fieldname;
print "sum: " sum;
# give out the number of lines that we processed
if(noheader==1)
{
totalnumberoflines = NR;
}
else
{
totalnumberoflines = NR -1
}
print "total lines (w/o header): " totalnumberoflines;
}
}