So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?
Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end. Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint
Awk in 20 Minutes (2015)
41–50 of 128 posts
Re: Awk in 20 Minutes (2015)
#42Earlier quoted context omitted.
Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end. Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint
If you just use awk for `{ print $2 }` then I would still prefer `tr -s ' ' | cut -d ' ' -f2`, since both are part of coreutils and with `awk` you would add a additional dependency to your script.
whereas, tr will still leave a trailing/leading space
for example:
$ echo ' a b c ' | tr -s ' ' | cut -d ' ' -f2
a
$ echo ' a b c ' | awk '{print $2}'
b
and by default awk splits on space/tab/newlines, whereas in cut example above, you get only space as delimitercut has its uses and will be faster than awk, but it depends on the problem being solved
Re: Awk in 20 Minutes (2015)
#43Earlier quoted context omitted.
Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end. Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint
> Awk '{ print $2 }' does LWSP gobbling That's like answering "why use Haskell?" with "Because its monads are monoids in the category of endofunctors"
Re: Awk in 20 Minutes (2015)
#44Re: Awk in 20 Minutes (2015)
#45Common lines between 2 files awk 'NR==FNR{a[$0]; next} $0 in a' colors_1.txt colors_2.txt http://archive.vn/mmd80
sort -m Re: Awk in 20 Minutes (2015)
#46 cat logfile | awk '/ERROR:/ {counts[$1] = counts[$1] + 1}; END { for (day in counts) print day " : " counts[day]}' | sort
I just needed to know how awk programs are structured, the rest is just simple programming!EDIT: I'm not sure if it's actually correct however...
Re: Awk in 20 Minutes (2015)
#47That said, there's an amazing amount you can do with it, if you really try. Someone once joked that I should try building an IRC bot in AWK, so I did: https://github.com/Marcus316/rufus
There's no pactical reason for it, but it was fun to play with the idea.
Re: Awk in 20 Minutes (2015)
#48Common lines between 2 files awk 'NR==FNR{a[$0]; next} $0 in a' colors_1.txt colors_2.txt http://archive.vn/mmd80
The link doesn't load for me, but that seems pretty unreadable. I'd probably sort, merge, and print duplicates: sort -m
# common lines
comm -12
regarding readability, it is the same with any new tool or programming language, you'd need to be familiar with its syntax and idioms, someone not familiar with command line and sort/uniq commands will find your solution as alien as wellRe: Awk in 20 Minutes (2015)
#49Every time I set out to use awk for some one time task I struggle to express what I need and end up saying fuck it and fire up vim and do exactly what I want with some quick macros. Am I the only one?
I usually say duck it and dust off my shiny Perl necklace
Re: Awk in 20 Minutes (2015)
#50Earlier quoted context omitted.
Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end. Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint
If you just use awk for `{ print $2 }` then I would still prefer `tr -s ' ' | cut -d ' ' -f2`, since both are part of coreutils and with `awk` you would add a additional dependency to your script.