Skip to content

Instantly share code, notes, and snippets.

@cdosborn
Created June 25, 2015 19:09
Show Gist options
  • Save cdosborn/92b0f373ab4364b27032 to your computer and use it in GitHub Desktop.
Save cdosborn/92b0f373ab4364b27032 to your computer and use it in GitHub Desktop.
Solutions to jpalardy's bash one-liner challenges, a tribute to gawk
| Solutions from https://github.com/jpalardy/Command-line-one-liner-challenges
|
| Connor Osborn
| 06/25/15
Number 12 is gone, because I thought the challenge was lame (and I like
unicode.)
1) gawk '{ wow[$1] += 1;} END { for (key in wow) print key, wow[key]; }' < $1
2) gawk '{ wow[$1] += 1;} END { for (key in wow) print key, wow[key]; }' < $1
3) gawk '{ wow[$1] += 1;} END { for (key in wow) print key, wow[key]; }' < $1
4) gawk 'BEGIN { RS=""; } { last = $0; } END { print last } ' < $1
5) cat $1 $2 | gawk '{ dict[$1] += 1 } END { for (key in dict) if (dict[key] > 1) print key }'
6) gawk '{ sum += $0 } END { print sum; }' < $1
7) cat $1 $2 | gawk '{ dict[NR] = $0 } END { while (i++ < NR / 2) print dict[i] "," dict[i + NR / 2] }'
8) gawk '{ for (i = 1; i <= NF; i++) if (tolower($i) ~ /\ythe\y/) totals++; } END { print totals } ' < $1
9) gawk '{ if (!longline) { longline = $0 } else { longline = longline "," $0 } } END { print longline }' < $1
10) cat $1 | while read line; do grep -q "^$line\$" $2; [[ $? -eq 1 ]] && echo $line; done
11) gawk 'length > max { max = length } END { print max }' < $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment