blob: 60013add65529619bf1dd05bc4db1f0e6606a1a4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# Search for alphanumeric words in a file
BEGIN {
# Name self
self = "gwp"
# Words are separated by any non-alphanumeric characters
FS = "[^a-zA-Z0-9]+"
# Nothing found yet
found = 0
# 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 (!length(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(msg) {
stderr = "cat >&2"
printf "%s: %s\n", self, msg | stderr
close(stderr)
exit(2)
}
# 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) }
|