5 ms·
Awk is a fantastic "swiss army knife", and always included (in some form) on every *nix. The article mentions FS but this is easier to access with -F. e.g. for
by kylek 7y ago
Awk is a fantastic "swiss army knife", and always included (in some form) on every *nix.
The article mentions FS but this is easier to access with -F. e.g. for some quick and dirty CSV munging-
awk -F, '{print $2}'
Something else I find super useful is the built-in regex matching, e.g.-
awk '/ERROR/{print $(NF-1)}'
or maybe picking out all 503's from an web server access log-
awk '/503/{print}'
edit: formatting....wow hn is bad for this
- jolmg 7y ago> awk '/503/{print}' {print} is the default action, I believe. So it would suffice to do: awk '/503/' Though, then you may as well just use grep for something like this.
- kylek 7y agoJust a basic example, I'd definitely use grep for something as simple as that. Only pointing out the pattern (you'll definitely have some heftier stuff in {} if you're going to bother with awk in that case)
- dredmorbius 7y agoThe typical awk case is to print selected fields, or operate on them (count, sum, classify, etc). So a '/<pattern>/ { print }' idiom is fairly common, generally on the way to something more refined.
- pizza234 7y agoFor some time I've been using both Awk and Perl for text processing, based on which offered the most convenient command for a given task. There are two (IMO) glaring omissions in Awk: - using the captured group(s) of a regular expression - slicing the arguments array Mawk supports the first functionality, but it's inconvient. Then at some point, I've found that virtually every task I do in my text processing via Awk can be done via Perl in a comparable way... and much more - including the two functionalities above - so I've ditched awk. The only thing inconvenient is to type something like `perl -lane` as opposed to `awk`, but this can be aliased :-)
- Scarbutt 7y agoAWK can be learned much faster than Perl though.
- webreac 7y agoYes, but as soon as your awk script becomes complex, you need to switch to a more complete language like perl (or python or ruby). Switch slightly earlier and avoid learning all the subtelities of awk (the same applies to sed). You need to learn well one scripting language and to know most common use cases of sed, awk.
- Annatar 7y agoNo you don't. There is no reason to switch. I've written many full-blown programs in AWK and I see no reason to switch them to another language -- they're fast, portable and readable, with tiny memory requirements. It all comes down to mastery: someone who's mastered AWK will have no issues with a large AWK program and therefore will feel no urge to translate that program to another language. Large AWK programs are easy to maintain because the syntax is so small that the code is always intrinsically understandable.
- mcguire 7y agoYou don't so much learn Perl as develop the ability to emulate Larry Wall.
- shaklee3 7y agoNot to mention perl is significantly faster. I was fairly bummed when I learned this since it felt ask is no longer that useful.
- tannhaeuser 7y agoTry mawk. It's an incredibly fast awk implementation. If you're using gawk, setting LANG=C will make large awk programs with lots of complex regexpes run significantly faster as well.
- tgv 7y ago> for some quick and dirty CSV munging- Tip: if you have quoted fields, use gawk, and FPAT = "([^,]+)|(\"[^\"]+\")";
- masklinn 7y ago> The article mentions FS but this is easier to access with -F. e.g. for some quick and dirty CSV munging- Though if you're doing a lot of csv munging, xsv would probably be a good investment.
- kylek 7y agoTIL! I know a couple other tools exist but this looks pretty nifty. May need to add it to my arsenal