4 ms·
Here is my solution for finding the tall males driving the blue honda with the semi-known licencse plate. I found awk quite suitable. awk 'BEGIN {RS="\n\n";
by Gorgor 11y ago
Here is my solution for finding the tall males driving the blue honda with the semi-known licencse plate. I found awk quite suitable.
awk 'BEGIN {RS="\n\n"; FS="\n"} $1 ~ /L337.*9$/ && $2 ~ /Honda/ && $3 ~ /Blue/ && $5 ~ /6'\''/ {print $4}' vehicles | cut -d' ' -f2,3
Edit: And to find the intersection between the SkyMiles members and the members of the Museum of Bash History, I used fgrep:
grep -Fxf Delta_SkyMiles Museum_of_Bash_History
- tremon 11y agoThat's great, and quite concise. I didn't get further than a sed query to filter the correct paragraphs, and then replace .* with a group filter (WR|DV|P8) to narrow the search: $ sed -rne '/L337.*9$/,/^$/p' vehicles
- Gorgor 11y agoI don’t understand what you gain in replacing .* with (WR|DV|P8) in a second query. Could you elaborate on that?
- tremon 11y agoWell, listing all the number plates that match /L337.*9/ will also return a number of other models and colours than the one we're looking for. So you could make the list smaller by only filtering on the license plates of correct models. But you're right that there's very little gained. The effort of reducing a list of 9 to a list of 4 is better spent on advancing the puzzle :)
- erostrate 11y agoYour 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!