21 ms·
Your first command uses 13 distinct non-alphanumeric characters: '{="\;}$~/.*& It looks more like black magic than code to someone like me who doesn't know awk
by erostrate 11y ago
Your first command uses 13 distinct non-alphanumeric characters: '{="\;}$~/.*&
It looks more like black magic than code to someone like me who doesn't know awk. It reminds me of these APL/J/K solutions I see on projecteuler.net. I imagine this is what happens when people completely ignore learning curve steepness and optimize for maximum productivity. Very impressive!
- derekp7 11y agoIt would look better if it wasn't all on one line (could be just the way HN is displaying it). Essentially, awk is a C like syntax with a bit of built in parsing, and a built in loop. It breaks up each input line into fields (by default using whitespace as a delimiter). The fields are dereferenced using the '$' character ($1, $2, etc). Then, for each line of the input, the entire program gets run. The awk program consists of a conditional (basically the body of an "if" statement -- the if is implied). If that conditional is matched, the C-like program segment following it (enclosed in curly braces) gets executed. And the contents of the curly braces is basically interpreted/scripted C. Two special conditions exist in awk -- BEGIN and END. They are evaluated (and the contents of their matching curly braces are executed) before, and after (respectively) any lines of input are read. Hope that helps give you a start on awk -- it is a really powerful tool. Edit: so as a quick walkthrough: Before any lines of the input text are read in, RS and FS variables get set. Then, for each line where field 1 matches the regular expression L337.*9$, and field 2 contains the word Honda, etc... it prints (outputs) the contents of field 4 (print $4). Ok, so in addition to normal C like syntax, this example contains regular expression matching too. But other than that (and some other variables that automatically get set, like NR for number of records, NF for number of fields in the current line), most of awk programs look like C.
- Gorgor 11y agoThanks for explaining. awk is actually quite simple, once you get the hang of it. It’s all on one line, because I used it as “one” command in bash. After all, this is the command line murders.
- JupiterMoon 11y agoMost if is is basically just an SQL type query on a text file. Awk actually has a fairly simple learning curve for queries like this, one can do stuff similar to this after a couple hours of playing around. EDIT: I couldn't find the tutorial I used but the digital ocean one seems like a good brief intro that would get you 90% of the way to understanding that awk command. https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-linux https://www.digitalocean.com/community/tutorials/how-to-use-... I agree that awk, sed etc look like line noise at first!
- cbd1984 11y agoUsing more characters/fewer distinct characters wouldn't make it any more readable. Similarly, COBOL is no more readable than Perl. Readability of texts written in programming languages comes from organization and knowing the definitions of terms and idioms, which is true of texts written in every language. Well-written software is difficult to read because terms have precise definitions which demand precise thought; philosophical texts approach this, but only mathematics replicates it. Therefore, shifting notation isn't going to help much.