News for hackers. Stuff that matters.
Re:Awk in 20 Minutes (2015) by mauvehaus on Friday May 01, 2020 @10:51PM (#23048714)
I learned perl in college in the 5.6 days, and did a lot of text processing with it for a time. At some point, I bit the bullet and learned awk, and I've mostly abandoned perl as a result.

Why? awk is small enough that it fits in my head, or at least the bits I need every couple of months do. And if I forget, it only takes 20 minutes to put them back in. perl, by contrast, is far too large to fit in my head and comes with an ecosystem that is much larger still.

I know I can use perl for what I use awk for, and when I've raised this point before, people have been quick to explain how to process input by lines, and conditionally do something. For basic stuff, the fact that that's basically all awk does[0] means there's so many fewer ways to do it wrong. I can't say the same of perl, even when I was more familiar with it.

caveat: awk, nawk, mawk, and gawk don't necessarily share the same set of corner cases, and you may not get error messages that make sense to you when you bump into one with an unfamiliar awk.

[0] I know, not really, and especially for gawk. I've written awk scripts a couple hundred lines in the past. It's true enough for the 20 minute version though.

Edited for clarity.

Re:Awk in 20 Minutes (2015) by AceJohnny2 on Friday May 01, 2020 @11:45PM (#23048962)
For my & others' reference (because I keep having to look it up, just like parent):

    perl -ne "print $_;"
'-n' will run the expression over each line of the input. This is the Awk-like mode.

    perl -pe "s/foo/bar/"
'-p' will run the expression over each line of input, and print out the (possibly modified) line. This is the Sed-like mode.

Slightly more info available in `man perlrun` or https://perldoc.perl.org/perlrun.html

Re:Awk in 20 Minutes (2015) by hf on Saturday May 02, 2020 @04:21AM (#23050283)
One particularly mnemonic collection of switches is 'plane':

    perl -plane 'my $script' 
which iterates over all files given on the command-line (or stdin) and

  + (p)rints every processed line back out
  + deals with (l)ine endings, in and out
  + (a)utosplits every line into @F
I am aware that -n and -p are mutually exclusive, but as -p overrides -n, it's seems simpler to just keep 'plane' in mind and remove the 'p' if necessary.
Re:Awk in 20 Minutes (2015) by forinti on Saturday May 02, 2020 @05:43AM (#23050601)
On the other hand, Perl is a lot faster.

One small test (with a big file) I executed took 1 minute with Perl and 20 minutes with awk.

And then there are those really complicated formats where awk is just not flexible enough.

Awk is really useful, but it doesn't cover the same problem set as Perl does.

Re:Awk in 20 Minutes (2015) by dan-robertson on Saturday May 02, 2020 @11:49AM (#23052412)
I’m a bit surprised by this. It surely depends on the use case. I tend to use awk for the same reasons as the parent (smaller to write and remember) and that the pattern-action structure works really nicely once you get used to it.
Re:Awk in 20 Minutes (2015) by yiyus on Saturday May 02, 2020 @01:40PM (#23053462)
That depends on what you are doing. Which test did you run?

Around 10 years ago I rewrote markdown.pl in awk and it was almost 20 times faster. The speedup came from both a much faster startup time and a much faster (albeit simpler) regexp implementation.

Re:Awk in 20 Minutes (2015) by DmitryOlshansky on Saturday May 02, 2020 @02:12PM (#23053700)
There are many AWK implementations, OSX I think have subpar AWK. GAWK was fast, last time checked.
Re:Awk in 20 Minutes (2015) by unhammer on Saturday May 02, 2020 @03:18PM (#23054178)
what script, and which awk?
Re:Awk in 20 Minutes (2015) by eternauta3k on Saturday May 02, 2020 @08:18AM (#23051303)
I'm tending towards Python for anything more than a single line shell script, because I find it easier to keep Python scripts tidy and well-structured. Not saying this is impossible, but I find the bash syntax for nontrivial operations hard to learn. This probably also applies if you replace bash with awk and python with perl.
Re:Awk in 20 Minutes (2015) by saberience on Saturday May 02, 2020 @08:12AM (#23051281)
What are you using Perl/awk for that it's needed so often?

I've been a software engineer since 2005 and worked my way up to being a VP of Engineering currently and never had to use either Perl or awk (or similar). I often read about these tools on Hackernews and I find it quite mystifying as I manage to have written Java, Scala, C#, SQL, and so on for 15 years and happily never needed them.

Is this a certain kind of engineering job that requires searching through text files so often and requiring specialized tools? I've managed my whole career with ctrl-f, and highlight-all matches.

Re:Awk in 20 Minutes (2015) by submeta on Saturday May 02, 2020 @09:55AM (#23051792)
There are hundreds of use-cases. It is not important what the use-cases are. - There are two types of developers: Those who use hacking to solve all kinds of tasks that need hundreds of manual steps. And those who never think of automating smaller steps. The second kind of devs can be very good software engineers, but they prefer IDEs instead of tweaking Vim or Emacs. The first kind of dev will look for possiblities to automate repeated steps, opportunities for tweaking, transforming, hacking. For the fun of it.
Re:Awk in 20 Minutes (2015) by dan-robertson on Saturday May 02, 2020 @11:46AM (#23052387)
I think this argument is unfairly dismissive.

I think it depends a lot on the sort of software one works with. A main use of awk and other unix tools for me is as-hoc data munging, looking through production logs, taking bits of data from different sources and comparing them. If you make eg gui applications and sell them or work as a contractor developing in-house applications for clients, you probably don’t see a lot of production logs like that or process them that way. Any data that you expect to process ought to go into a database and then you can use sql (where joins work much better than the unix join command and you can use the actual structure of the data instead of trying to tease it out with the smallest simplest code you can think of). You might be using a debugger or backtracks or reproduction in test to deal with/investigate production issues rather than starting with logs. On the other hand, many other companies will have big production systems that run on lots of different virtual machines and produce lots of logs and have issues that are hard to reproduce (or maybe the logs are easier), and have lots of data spread about random places such that it may be easier to do the hacky thing for one-off cases or to prototype or whatever rather than Doing things more thoroughly or properly. And this set up will also lead to tools that are designed to fit into the rest of the unix ETL tack by the way they output or input data.

On the second point, I think that even as a die-hard emacs user I wouldn’t recommend that anyone try to use emacs as an ide for something like java (or maybe c++). I’d probably try to use emacs a myself but I think my java-editing experience would be worse than for those people who do it with an IDE. More realistically, I’d try to avoid writing java if at all possible.

Re:Awk in 20 Minutes (2015) by masto on Saturday May 02, 2020 @10:48AM (#23052084)
In all that time, have you strictly been writing code, on computers that came set up out of the box, that runs on computers managed by other people? This is not an insult, I’m aware such things exist, but I seem to travel in different circles. It may come from a certain era of being into computers as a hobby for a long time before I could call myself a software engineer, and having to do what we now call DevOps necessarily by working for companies that didn’t have the resources to hire a fleet of system administrators. As a result I may live in a bubble where the friends and colleagues I associate with are as comfortable with a command line as they are with an IDE, and frequently jump between the two.

I mention DevOps because it is hard to think of a situation where you’re just coding Java and C# all day that truly requires awk-or-similar. But just the other day I had a situation where I copied the contents of a CD-ROM to a web server — drag and drop on my Mac — and ended up with the files all in upper case. The program I was trying to run was failing because it requested everything with lower-case URLs. There were hundreds and hundreds of files in a bunch of directories, so I fixed it with Perl in about 30 seconds.

I’m curious if that’s the sort of problem you never encounter, or if it is, what you reach for.

Re:Awk in 20 Minutes (2015) by Timpy on Saturday May 02, 2020 @08:52AM (#23051458)
I'm sure use cases vary to some degree, but awk clicked for me when I had some data that wasn't formatted well. I didn't use it in a script, I just worked in the shell and kept iterating until it looked good, then redirected it into a text file. I use it for quick one-offs like this all the time now.

I've also used awk to get button presses from an input stream on a MIDI controller. For me, I found that the up front cost of learning a few awk commands quickly made it's return on investment.

Re:Awk in 20 Minutes (2015) by mauvehaus on Saturday May 02, 2020 @10:29AM (#23051983)
This. It's often a good enough tool for solving problems that technically require a specialized tool. Except learning the specialized tool takes an order of magnitude more time than iterating on awk to get a good-enough solution.

I'm not proud to admit it, but I've used awk in a couple places where I should have properly used `expect' instead. Except, I don't know expect well and haven't gotten to the point where it was worth the investment in learning it.

Re:Awk in 20 Minutes (2015) by eternauta3k on Saturday May 02, 2020 @08:15AM (#23051289)
> I've managed my whole career with ctrl-f, and highlight-all matches.

Imagine you have to do this for 100 files.

Re:Awk in 20 Minutes (2015) by saberience on Saturday May 02, 2020 @09:24AM (#23051637)
Fair :)
Re:Awk in 20 Minutes (2015) by scruple on Saturday May 02, 2020 @09:59AM (#23051812)
Are you working in a Linux development environment? Targeting Linux systems? You're not going to come across much of a use for awk if you're in the Windows world. I'm making an assumption based on C#.

I've used perl and awk extensively for all sorts of things. Log parsing embedded device logs to generate reports was a big one for me, that's where I really learned both languages. And they are superb tools for that task. We also used awk quite a bit for configuration parsing as a sort of intermediary between different processes and scripts on those devices.

As I moved into web development, I've found myself using both much less. I haven't touched perl more than once in the last 6 years -- I was given a script to maintain recently but it only needed an hour or two of my time to add a small feature to. That perl script is part of a legacy build system. I still use awk every couple of months but it's just part of some pipeline on the command line.

Re:Awk in 20 Minutes (2015) by Wilem82 on Saturday May 02, 2020 @09:44AM (#23051737)
Using any kind of management position as justification for anything tech-related won't have the effect you're probably aiming for. Dev going management is always downshifting.
Re:Awk in 20 Minutes (2015) by kshacker on Saturday May 02, 2020 @12:43PM (#23052946)
So I just wrote one for personal use. I was looking for a duplicate file/photo finder and read some reviews about losing data so I said why would I trust a third party for my data and wrote a dupe finder. Use a hash (md5) to make a match (it is not perfect but it works) and then sort and print to show me the dupes. The first version was 45 lines of code although I could have reduced lines if I tried. You don't need awk strictly but I used it to prettify my output and add some filters. Current code with lots more bells and whistles is less than 150 lines (including white space) of bash and awk. And it works for me and I trust it.

I could set up a database, write multiple layers of code ... but really?

Re:Awk in 20 Minutes (2015) by indymike on Saturday May 02, 2020 @09:58AM (#23051811)
Well, you might have have got the job done faster with awk (and other tools like it)...

ETL and anything dealing with importing large data sets come to mind.

Re:Awk in 20 Minutes (2015) by mauvehaus on Saturday May 02, 2020 @10:24AM (#23051958)
I don't, and that's half the problem :-) If I did, I'd probably remember enough perl that I'd have never bit the bullet and learned awk!

To answer the serious question, it's often enough for a first order solution to any problem where you have line and field separated text. Pulling instances of "something weird" out of log files, for instance, is a great use for awk, especially if the fingerprint of "something weird" is scattered through fields in a line in a way that makes grep cumbersome. Or if it spans a couple of lines, especially if you're dealing with a log from a multi-threaded app where there might be irrelevant lines interleaved with the ones you care about.

I haven't written much production-grade awk, but I've often used it as a tool to understand a problem well enough to write a production-grade solution to a problem or fix bugs in application code.

Re:Awk in 20 Minutes (2015) by andrewl on Saturday May 02, 2020 @01:47PM (#23053514)
The author offers one answer to this in his opening text:

I say it's useful on servers because log files, dump files, or whatever text format servers end up dumping to disk will tend to grow large, and you'll have many of them per server. If you ever get into the situation where you have to analyze gigabytes of files from 50 different servers without tools like Splunk or its equivalents, it would feel fairly bad to have to download all these files locally to then drive some forensics on them.

This personally happens to me when some Erlang nodes tend to die and leave a crash dump of 700MB to 4GB behind, or on smaller individual servers (say a VPS) where I need to quickly go through logs, looking for a common pattern.

Re:Awk in 20 Minutes (2015) by DLA on Saturday May 02, 2020 @03:55PM (#23054419)
You ever deal with very large sets of large text files? You really CTRL-f through multi-GB files? Have you never needed to transform data between formats or yank out subsets of data for processing? You may have just working on business systems that did not deal with significant data volumes or data you did not control (coming from some other organization, for example).
Re:Awk in 20 Minutes (2015) by mongol on Saturday May 02, 2020 @08:32AM (#23051370)
Analysis of large log files. Granted, today Splunk and other tools are more used.
Re:Awk in 20 Minutes (2015) by scottlocklin on Saturday May 02, 2020 @11:05AM (#23052154)
People (me anyway) used perl back in the dark ages of the 90s for the same sorts of things people use python for now. It was really the only option for open source general purpose interpreters for quite a while. People still use it in biotech I think, or were 5 years ago; the text processing capabilities are useful for genomic data.

Awk/sed and the ETL stack that comes with every Unix-like OS (aka od, tr, cut, sort) and all that are superb data wrangling tools. Log file parsing, data cleaning, even actual data science at scale can be done with these tools. They're extremely efficient, as they're designed from an era when pretty much all interesting data was comparable to or much larger than memory size. As such, you can do a lot of stuff with them that most people don't imagine is even possible. FWIIW for high end data scientists; I don't consider knowledge of these tools to be optional at all. Anyone who hasn't used them in their career hasn't worked on serious problems, and is probably the kind of educated idiot who will suggest you do a job in a giant Hadoop cluster that you could easily do on one machine.

Re:Awk in 20 Minutes (2015) by mrmrcoleman on Saturday May 02, 2020 @02:09PM (#23053675)
I read the first line as "I learned perl in college in 5.6 days"
Re:Awk in 20 Minutes (2015) by known on Saturday May 02, 2020 @02:27AM (#23049785)
Perl is Awk on steroids :)
Re:Awk in 20 Minutes (2015) by Torwald on Saturday May 02, 2020 @06:48AM (#23050940)
Perl 6 is when they then took LSD and at a hang-up, they went to the clinic and came back a s two persons. One said: never again, the other: bring it on.
Re:Awk in 20 Minutes (2015) by lizmat on Saturday May 02, 2020 @10:22AM (#23051946)
Cute analogy. In fact, one said: you're not me. And the other ultimately said: I'm Raku.

In case you didn't know: Perl 6 has been renamed to Raku, using the #rakulang tag on social media.