Hacker News new | past | comments | ask | show | jobs | submit login

If I see that regex in code somewhere, I'd have to stop and break it down the way you did to understand what it's doing, and I've spent a fair bit of time with regex. I think reading it and understanding what it does in one go would require far more than mere familiarity. (and that's not including the awk-isms like FNR etc.)



“That regex” makes me suspect that you think that the entire script is a regex. It’s not. It’s an Awk script, with two regexes in it.

First, know that Awk goes line by line. The script is implicitly executed for each line in the input. That’s just the entire thing Awk does, normally—if you want to process files line by line, and your needs are simple, well, Awk fills in the gaps where stuff like “cut” fall short (and I can never remember how to use cut, so I just use Awk anyway).

Second, know that “if” is implicit in Awk. You don’t write this:

  if (condition) { code }
You write this instead:

  condition { code }
This is like how Sed works, or Vim, except you get braces and the syntax is a bit easier to read.

The code block contains two statements: one function call (gsub) and then print.

So the first regular expression is just “//d ”, with some escaping for the slashes. The second regular expression is “.*//d ”. I do think that someone with basic familiarity with regexes should have no problem understanding these.


Cool! Now do this with every line of awk you're asking someone else to maintain, give up, and then write it in a better, more explicit language instead.


Feels like I’m on Usenet again when I read comments like this.

Awk is mostly nice for one-liners and is something you can just write into a command-line ad-hoc. It’s good at that. I could write the same thing as a Python script but it would take longer, and I would need to know that Python is installed on the system—Awk has a larger install base and is found on “minimal” installs.

If you hate Awk, and think it’s stupid, don’t use it. Seems like a big waste of time trying to argue with people who like Awk. That’s the kind of discussion I remember from my experience on Usenet in the 90s, and it seems like some people haven’t learned to move on.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: