-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegrep.awk
102 lines (91 loc) · 1.94 KB
/
egrep.awk
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# egrep.awk --- simulate egrep in awk
#
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
# Options:
# -c count of lines
# -s silent - use exit value
# -v invert test, success if no match
# -i ignore case
# -l print filenames only
# -e argument is pattern
#
# Requires getopt and file transition library functions
@include "getopt"
@include "ftrans"
BEGIN {
while ((c = getopt(ARGC, ARGV, "ce:svil")) != -1) {
if (c == "c")
count_only++
else if (c == "s")
no_print++
else if (c == "v")
invert++
else if (c == "i")
IGNORECASE = 1
else if (c == "l")
filenames_only++
else if (c == "e")
pattern = Optarg
else
usage()
}
if (pattern == "")
pattern = ARGV[Optind++]
for (i = 1; i < Optind; i++)
ARGV[i] = ""
if (Optind >= ARGC) {
ARGV[1] = "-"
ARGC = 2
} else if (ARGC - Optind > 1)
do_filenames++
# if (IGNORECASE)
# pattern = tolower(pattern)
}
#{
# if (IGNORECASE)
# $0 = tolower($0)
#}
function beginfile(junk)
{
fcount = 0
}
function endfile(file)
{
if (! no_print && count_only) {
if (do_filenames)
print file ":" fcount
else
print fcount
}
total += fcount
}
{
matches = ($0 ~ pattern)
if (invert)
matches = ! matches
fcount += matches # 1 or 0
if (! matches)
next
if (! count_only) {
if (no_print)
nextfile
if (filenames_only) {
print FILENAME
nextfile
}
if (do_filenames)
print FILENAME ":" $0
else
print
}
}
END {
exit (total == 0)
}
function usage()
{
print("Usage: egrep [-csvil] [-e pat] [files ...]") > "/dev/stderr"
print("\n\tegrep [-csvil] pat [files ...]") > "/dev/stderr"
exit 1
}