Neat discussions around that sort of thing at least here: https://news.ycombinator.com/item?id=23427479
$ printf "a b c d e\n1 2 3 4 5" | perl -lanE 'say "$F[2] $F[4]"'
c e
3 5It turns out though that this is because Perl and later Ruby were inspired by AWK and even support these line by line processing idioms with BEGIN and END sa well.
ruby -n -a -e 'puts "#{$F[0] $F[1]}"'
ruby -ne '
BEGIN { $words = Hash.new(0) }
$_.split(/[^a-zA-Z]+/).each { |word|
$words[word.downcase] += 1 }
END {
...However, I feel I really should have taken the time to learn Awk better as it could probably be done there, and simply! (It was a good excuse to tinker with rust, but that's an aside.)
$ awk -F: '{len=length($1);if(len>max){max=len;user=$1}}END{print user,max}' /etc/passwd echo -e "foo bar baz" | choose -1 -2
vs awks echo -e "foo bar baz" | awk '{ print $2, $3}'
I love the effort people are putting into reinventing the core unix tools.I think I'll stick with Awk for now though.
$ choose
bash: choose: command not found...
ls -l | tr -s ' ' | cut -d ' ' -f 5 $ printf " one two three" | tr -s ' ' | cut -d ' ' -f 1
$ printf " one two three" | awk '{print $1}'
one ps ax | sed 's/^\s\+//; s/\s\+/ /g;' | cut -d ' ' -f 4 echo -e '1\t2\t3\t4\t5' | expand -t 1 | cut -d ' ' -f 3Since then I've made a point of finding man-pages from other systems whenever the manual for a GNU tool is a bit daunting. It tends to lower the learning threshold quite a lot, honestly.
$ man gawk | wc
1568 13030 94207
$ man -l /usr/share/man/man1/awk.1plan9.gz | wc
214 1579 10956
Not trying to detract from this great guide. Just a general tip :)But once I got the idea of aggregating the book review data from amazon I felt I had to see it through.
When I wrote my introduction to JQ someone mentioned JQ was tricky but super-useful like AWK. I nodded along with this, but actually, I had no idea how Awk worked.
So I learned how it worked and wrote this up. It is a bit long, but if you don't know Awk that well, or at all, I think it should get the basics across to you by going step by step through examining the book reviews for The Hunger Games trilogy.
Let me know what you think. And also let me know if you have any interesting Awk one-liners to share.