aboutsummaryrefslogtreecommitdiff
path: root/bin/gwp.awk
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-12-04 14:18:25 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-12-04 14:23:23 +1300
commit455d79db130768a7fae8e17d222f49d1466e5b46 (patch)
tree0b91148dd76f5922ff8108ff9bc8948adcee0ebe /bin/gwp.awk
parentTweaks to kvlt(6df) (diff)
downloaddotfiles-455d79db130768a7fae8e17d222f49d1466e5b46.tar.gz
dotfiles-455d79db130768a7fae8e17d222f49d1466e5b46.zip
Add gwp(1)
Diffstat (limited to 'bin/gwp.awk')
-rw-r--r--bin/gwp.awk57
1 files changed, 57 insertions, 0 deletions
diff --git a/bin/gwp.awk b/bin/gwp.awk
new file mode 100644
index 00000000..db2764ec
--- /dev/null
+++ b/bin/gwp.awk
@@ -0,0 +1,57 @@
+#!/usr/bin/awk -f
+# Search for alphanumeric words in a file
+BEGIN {
+
+ # Name self
+ self = "gwp"
+
+ # Words are separated by any non-alphanumeric character
+ FS = "[^[:alnum:]]"
+
+ # First argument is the word required; push its case downward so we can
+ # match case-insensitively
+ word = tolower(ARGV[1])
+
+ # Blank the first argument so Awk doesn't try to read data from it as a file
+ ARGV[1] = ""
+
+ # Bail out if we don't have a suitable word
+ if (!word)
+ fail("Need a single non-null alphanumeric string as a search word")
+ if (word ~ FS)
+ fail("Word contains non-alphanumeric characters; use grep(1)")
+}
+
+# Bailout function
+function fail(str) {
+ printf "%s: %s\n", self, str > "/dev/stderr"
+ exit(1)
+}
+
+# If there's more than one filename, precede the print of the current line with
+# a filename, a colon, and a space, much like grep(1) does; otherwise just
+# print it
+function fnpr() {
+ if (ARGC > 3)
+ print FILENAME ":" OFS $0
+ else
+ print
+}
+
+# Iterate through the words on this line and if any of them match our word,
+# print the line, and flag that we've found at least one word; once a single
+# instance of the word is found, just print and continue on to the next line
+{
+ for (i = 1; i <= NF; i++) {
+ if (tolower($i) == word) {
+ found = 1
+ fnpr()
+ break
+ }
+ }
+}
+
+# Exit zero if we found at least one match, non-zero otherwise
+END {
+ exit(!found)
+}