awk -F":" 'BEGIN{total=0}{if($3>240)total+=$3}END{print total}' /etc/passwd
awk -F":" 'BEGIN{total=0}{if($3>240)total+=$3}END{print total}' /etc/passwd
perl -e 'while(<>) { $tot += (split)[2]; }; print "$tot\n"'
It's funny to see that in perl's decline, the stuff it did well is being forgotten and resurrected via tools it at one point had mostly replaced.If my codebase wasn't already in perl I wouldn't necessarily be able to benefit from a large library of my own functions relevant to the problem, but at least I would still have CPAN to fall back on. I'm not sure awk will ever be able to compete with that.
perl -F: -anE '$tot+=$F[2]}{say$tot' perl -F: -anE '$_=$F[2];$t+=$_if$_>240}{say$t'
I wanted to be clever by doing `$t+=$_*($_>240)`, but that's actually one byte larger. :( perl -F: -lane '$total+=$F[2] if $F[2]>240; END{print $total}' /etc/passwdMe too. For example, number of unique IP requests in a log file in milliseconds ;)
$ awk '{ print $1 } ' caddy_log | sort | uniq | wc -l
But rarely compose my own from scratch. It's mostly copy paste. And store in admin bin for future use.
$ cat duplicates.txt
abc 7 4
food toy ****
abc 7 4
test toy 123
good toy ****
$ awk '!seen[$2]++' duplicates.txt
abc 7 4
food toy ****
$ awk '!seen[$2]++{cnt++} END{print +cnt}' duplicates.txt
2I'll add a note, thanks :)
awk '{ip[$1]=1}; END{ print length(ip) }'
Use the hash tables directly, assuming you've got the memory. That should be slightly faster as it avoids the sort.also, {ip[$1]} is enough...
;-)
BEGIN{total=0}
can be skipped (at least in the awk's I've used, not sure if some more recent / strict awk differs), because awk initializes the variable total to 0.
I remember this because I do this all the time to quickly get the (non-recursive) sum of the sizes of the files in a directory - it's pretty much muscle memory from a while now:
ls -l | awk '{ s += $5 } END { print s/1024 " KB" }'
For recursive size, one can use ls -lR or the du command with various options according to need.
So in other words, we can take out the BEGIN block, but then we must remember to change print total to print total + 0.
Also, using uninitialized variables is basically a code golfing stupidity that will bite you in any halfway complicated program.
GNU Awk has a useful --lint argument which spots uses of uninitialized variables. If you make it habit to write code that way, if you then use --lint for finding a bug, you have to deal with false positives.
Nonsense. Not if you know what you are doing, and used it in a known way, which is what I did. The code I wrote works. I tested it on Linux before posting it. Also, such a usage (skipping the initializer) is mentioned (IIRC) in the classic Kernighan & Pike book "The Unix Programming Environment" (still a great resource, though not updated for modern Unix/Linux features), which is where I learned it from, years ago (and hence why I qualified my statement by saying it may not work in more strict or modern awk versions). Fine to talk about other variations but it does not mean that my variation is wrong.
Don't try to read my mind. My intention was not code golfing. Was just sharing some fun info. It's not a big deal to keep the initializer either, I'm quite aware of that.
Your intention can be understood as the promotion of code golfing, as evidenced by these words:
The fragment BEGIN{total=0} can be skipped
by which you're clearly encouraging that other coder to make their code shorter by removing an initialization that works fine.
Your statement (above) can be "understood" as not understanding my prior statement(s), including the one in which I said "Don't try to read my mind" (w.r.t. intention, because you cannot - it is mine (mind), not yours). If you cannot grok that after a second explanation, I have nothing further to say. Good day.
Smile when you say that, stranger!
http://www.thisdayinquotes.com/2011/05/when-you-call-me-that...
#!/bin/sh
x=0;while true;do read a;
test ${#a} -gt 0||exec echo $x
a=${a#*:\*:};n=${a%%:*};
test $n -le 240||x=$((x+n));
done < /etc/passwd #!/bin/sh
IFS=:;x=0;while true;do read a b c d;
test ${#a} -gt 0||exec echo $x
test $c -lt 240||x=$((x+c));
done < /etc/passwd awk -F: '$3>240{total+=$3} END{print +total}' /etc/passwd