I use awk because there's an almost 100% chance that it's going to be installed on any unix system I can ssh into. I use awk because I like to visually refine my output incrementally. By combining awk with multiple other basic unix commands and pipes, I can get the data that I want out of the data I have. I'm not writing unit tests or perfect code, I'm using rough tools to do a quick one-off job. For instance, "mail…
I disagree, it's quite elegant if you think in terms of relational algebra operators: * Projection (Π): awk and cut for simple cases * Selection (σ): grep for simple cases, otherwise sed & awk * Rename (ρ): sed * Set operators: join, comm...
Why Learn Awk? (2016)
181–190 of 246 posts
Re: Why Learn Awk? (2016)
#182So why for example Awk instead of let's say Perl? The argument of "it's likely to be installed" isn't very compelling.
For a huge number of simple tasks, awk is available and sufficient. It's largely a subset of Perl, so yes, there's some skills overlap, but there are times where knowing awk is the right tool and the available tool will pay off.
Re: Why Learn Awk? (2016)
#183It would be so great if awk had a csv mode. For whatever reason (Excel), CSV seems to be the default text format for field oriented data. Maybe I’m dumb but I’ve never come up with a separator regex that is quite right.
FS = ","
or: awk -F ','
If you're working with CSV data that has quoted strings with embedded commas, FPAT is your friend: FPAT = "([^,]+)|(\"[^\"]+\")"
See: https://www.gnu.org/software/gawk/manual/gawk.html#Splitting...Re: Why Learn Awk? (2016)
#184But it was the ability to string together these types of command line tools that made it possible.
Re: Why Learn Awk? (2016)
#185So why for example Awk instead of let's say Perl? The argument of "it's likely to be installed" isn't very compelling.
If you're versed in both, it comes down to taste, though there really are cases where you'll have awk (usually via busybox) but not Perl. OpenWRT comes to mind (just verified it's not present by default, though yes, packages are available). For a huge number of simple tasks, awk is available and sufficient. It's largely a subset of Perl, so yes, there's some skills overlap, but there are times where knowing awk is th…
Re: Why Learn Awk? (2016)
#186Earlier quoted context omitted.
> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Does `cut -f2` not work? My complaint with cut is that you can't reorder columns (e.g. `cut -f3,2` ) Awk is really great for general text munging, more than just column extraction, highly recommend picking up some of the basics Edit to agree with the commenters below me: If the file isn't delimited with a single character, then cut alon…
It does not. Compare: $ echo ' 1 2 3' | cut -f2 1 2 3 $ echo ' 1 2 3' | cut -f2 -d' ' $ echo ' 1 2 3' | awk '{print $2}' 2 "-f [...] Output fields are separated by a single occurrence of the field delimiter character."
Re: Why Learn Awk? (2016)
#187Earlier quoted context omitted.
I know you’re not asking for awk protips but you can prefix the block with a match condition for processing. ... | grep foo | awk ‘{print $6}’ | ... becomes ... | awk ‘/foo/{print $6}’ | ... If you start working this into your awk habits you’ll find delightful little edge cases that you can handle with other expressions before the block (you can, for example, match specific fields).
No one has mentioned changing the default field separator, e.g., awk FS=: '{print $1}' instead of cut -d: -f1 awk FS=" " '{print $1}' instead of cut -d'>' -f1
echo test,123 | awk -F, '{print $1}'
Re: Why Learn Awk? (2016)
#188Earlier quoted context omitted.
> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. I'm not sure if you refer spefically to cut, but Perl has something similar and approximaly terse: > echo 'a b c' | perl -lane 'print $F[1]' Also, Perl can slice arrays, which is something that I really miss in Awk.
PERL is bloatware by comparison and less likely to be installed on distros than AWK. (e.g, embedded or slim distros. that's why you rarely see nonstandard /bin execs in shell scripts).
Re: Why Learn Awk? (2016)
#189Earlier quoted context omitted.
I don't suppose your dotfiles are available anywhere. I'm just wondering what other useful things I can steal ;)
I don't have my dotfile here (on my phone) but here's some ideas from things I've aliased that I use a lot: cdd : like cd but can take a file path - : cd - (like 1-level undo for cd) .. : cd .. ... : cd ../.. (and so on up to '.......') nd : like cd but enter the dir too udd : go up (cd ..) and then rmdir xd : search for arg in parent dirs, then go as deep as possible on the other branch (like super-lazy cd, actually…
Re: Why Learn Awk? (2016)
#190Earlier quoted context omitted.
No one has mentioned changing the default field separator, e.g., awk FS=: '{print $1}' instead of cut -d: -f1 awk FS=" " '{print $1}' instead of cut -d'>' -f1
No need to explicitly set FS! Just use: echo test,123 | awk -F, '{print $1}'
awk 'BEGIN {FS=":"};{print $1}'
One benefit of the FS variable over -F, at least in original awk, is that by using FS the delimiter can be more than one character. I guess that's why I remember FS before I remember -F. More flexible.