Why Learn Awk? (2016)
111–120 of 246 posts
Re: Why Learn Awk? (2016)
#112Re: Why Learn Awk? (2016)
#113Awk is a command I turn to time and again. For me it's the single most valuable command for enabling the piped single-purpose pattern. As an example, if I want a sorted list of all open files under the home directories on CentOs I can do this: lsof | awk '{ print $10 }' | grep ^/home/ | sort | uniq
Re: Why Learn Awk? (2016)
#114Earlier quoted context omitted.
And awk doesn't offers cut's column range selection ;)
Absolutely. Everything comes with costs & benefits. But I'm not sure I've, in my entire 23-year professional programming career, ever encountered a fixed-width text format in the wild. I've used cut even so for places where by coincidence the first couple of columns happened to be the same size, but that's really a hack. Obviously, other people have different experiences which is why I quality it so. (I only narrowly…
Re: Why Learn Awk? (2016)
#115Because for some bizarre reason, "cut" doesn't ship with any decent column selection logic that is the equivalent of awk's $1, $2, etc., even in 2020. That's like 90% of my use of awk right there. I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Posted partially so the Internet Correction Squad can froth at the mouth and set me straight, because I'd love to be showed to be wrong here.
You can submit a new feature request/patch to GNU coreutils' cut, but they'll probably just tell you to use awk.
Edit: Nevermind, it's already a rejected feature request: https://lists.gnu.org/archive/html/bug-coreutils/2009-09/msg... (from https://www.gnu.org/software/coreutils/rejected_requests.htm...)
Re: Why Learn Awk? (2016)
#116in everyday life however, many small problems are a bit too much for the shell but too little for Perl/Python/whatever.
awk fits very nice in there.
Re: Why Learn Awk? (2016)
#117Awk, like shell, is a fraught programming environment, full of silent failure and hidden gotchas. Even for one-liners. I use shell because I have to, not because I like it. I dread maintaining shell scripts which have a bunch of awk and sed in them. The Unix ideal of small single-purpose tools and text processing is separable from these old warhorses.
scripts are the antithesis of modern software development. No Unit testing, No CI, no monitoring, no consistency, often no source control. Appeals to me as a hacker but only my scripts are intelligible - which is a bad sign. :)
I have written a tool (also a script) myself that allows you to write unit tests, manage your scripts in git, load scripts in other scripts, etc. Maybe I will post a Show HN in the coming weeks, but at the moment I would like to round up some edges before posting it.
In my experience, the biggest problem is that there are many different runtime environments out there that differ in detail and make it hard to write scripts that run everywhere. But programmers not applying all their skills (e.g. writing tests) to build scripts are also part of the problem.
Re: Why Learn Awk? (2016)
#118Last week I threw out AWK and replaced it with Ruby (Could've been Python, Perl or PHP even). Because AWK is not suited for CSV. Please prove me wrong! I had to parse 9million lines. Some of which contain "quoted records", others, same column, are unquoted. Some contain comma's, in the fields, most don't. CSV is like that: more like a guideline than actual sense. Two hours of googling and hacking later, I gave up and…
I usually load CSV data into PostgreSQL to do anything with it; mostly wrote this Awk library for fun. So I'm not going to argue that Awk is the best language for doing this kind of thing, but it is possible.
Re: Why Learn Awk? (2016)
#119Skip learning of sed and awk and jump straight to perl instead. $ perl --help ... -F/pattern/ split() pattern for -a switch (//'s are optional) -l[octal] enable line ending processing, specifies line terminator -a autosplit mode with -n or -p (splits $_ into @F) -n assume "while ( ) { ... }" loop around program -p assume loop like -n but print line also, like sed -e program one line of program (several -e's allowed,…
Because syntatically as a language/tool it is super easy to remember. Writing one liners with awk feels more intuitive to me. Awk example: ls -l | awk '{print $9, $5}' or ls -lh | awk '{print $9, $5}' Seems a whole lot simpler. To me. I find if you have to write exhaustive shell scripts then maybe you can look for something more verbose like Perl, I guess.
Re: Why Learn Awk? (2016)
#120I 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…
... | 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).