3 ms·
I think my hiccup is that it seems like good awk programmers are expected to be able to pass the full bloody program in as a simple string argument in a bash on
by taeric 8y ago
I think my hiccup is that it seems like good awk programmers are expected to be able to pass the full bloody program in as a simple string argument in a bash one-liner. Which is clearly not tenable.
There are a few things I have done which felt more difficult than they should. However, the vast majority of dealing with structured data using awk really has been a pleasant surprise.
- asicsp 8y agoone-liners are fun though, especially if you like code golfing.. if you're interested, you could give my tutorial(https://github.com/learnbyexample/Command-line-text-processing/blob/master/gnu_awk.md https://github.com/learnbyexample/Command-line-text-processi...) a try it has over 300 examples..
- ageofwant 8y agoThanks for that mate. I'm a regular customer your work is greatly appreciated.
- asicsp 8y agothanks for the feedback :) happy learning
- natecavanaugh 8y agoI gotta say, I really enjoyed skimming through this, and saving it for future reference. I can already tell there are some basic things that I should've memorized with all the one liners I've used, but because most were cobbled together for the immediate task (and many online forum searches assume a lot of prior knowledge), so t this presentation, and cookbook style references really help me hold the practical benefits in memory alot longer, so thanks again!
- asicsp 8y agothanks, that means a lot to me :) if you are interested in text processing in general, do check the other chapters for grep/sed/perl/sort/paste/pr/etc I learned a lot writing them up and marvel at the speed and functionality provided by these tools.. for many cases, it is lot easier to write a bash script combining these tools than to write a Perl/Python/Ruby script..
- xpil 8y agoAhh, what a useful link! Bookmarking immediately. I use AWK like once a year but when I do, I usually struggle a lot. Your examples are really helpful!
- asicsp 8y agothanks, it's a reference for me as well - not easy to remember all the features and tricks
- schoen 8y agoI think some of the examples in popular documentation use awk -f to read a program from a file, so I don't feel like that's regarded as an unreasonable way to run awk code. It's true that many programs might well just have a single action (or a single action plus a BEGIN and END), but I'm not sure that resorting to awk -f will give great offense.
- coliveira 8y agoAwk makes it easy to write one liners, but it is also very easy to save your script to a file and run it with the -f option.
- Stratoscope 8y agoAwk did have this reputation of hard-to-read one liners, but it was also always possible to write readable code in it much as you would do in any language. My favorite bit of Awk code that I ever wrote was this print formatting program for the HP LaserJet II: https://github.com/geary/awk/blob/master/LJPII.AWK https://github.com/geary/awk/blob/master/LJPII.AWK It printed source code in a "2-up" format, with two pages printed side by side in landscape mode. It looked for the kind of separators that I and many people were fond of back in those days (the source code is a good example) and converted those to graphic boxes. And it tried to be smart about things like breaking pages before a comment box, not after. I wish I still have a sample printout handy. For 300 lines of what I thought was fairly cleanly written Awk code, it made some nicely formatted printouts.
- oblio 8y agoThat's very clean looking code and I think it would be considered so regardless of the language it was written in, IMHO. Nice! :)
- pmarin 8y agowhat I usually do is to wrap the awk script in to a sh function. I never do one-liners unless it is a well known idiom.