I am trying to sum certain numbers in a column using awk
. I would like to sum just column 3 of the "smiths" to get a total of 212. I can sum the whole column using awk
but not just the "smiths". I have:
awk 'BEGIN {FS = "|"} ; {sum+=$3} END {print sum}' filename.txt
Also I am using putty. Thank you for any help.
smiths|Login|2
olivert|Login|10
denniss|Payroll|100
smiths|Time|200
smiths|Logout|10
Best Answer
-F
flag sets the field separator; I put it in single quotes because it is a special shell character.$1 ~ /smiths/
applies the following {code block} only to lines where the first field matches the regex/smiths/
.Note that since you're not really using a regex here, just a specific value, you could just as easily use:
Which checks string equality. This is equivalent to using the regex
/^smiths$/
, as mentioned in another answer, which includes the^
anchor to only match the start of the string (the start of field 1) and the$
anchor to only match the end of the string. Not sure how familiar you are with regexes. They are very powerful, but for this case you could use a string equality check just as easily.