source: vendor/gawk/3.1.5/awklib/eg/prog/egrep.awk@ 3076

Last change on this file since 3076 was 3076, checked in by bird, 19 years ago

gawk 3.1.5

File size: 1.9 KB
Line 
1# egrep.awk --- simulate egrep in awk
2#
3# Arnold Robbins, [email protected], Public Domain
4# May 1993
5
6# Options:
7# -c count of lines
8# -s silent - use exit value
9# -v invert test, success if no match
10# -i ignore case
11# -l print filenames only
12# -e argument is pattern
13#
14# Requires getopt and file transition library functions
15
16BEGIN {
17 while ((c = getopt(ARGC, ARGV, "ce:svil")) != -1) {
18 if (c == "c")
19 count_only++
20 else if (c == "s")
21 no_print++
22 else if (c == "v")
23 invert++
24 else if (c == "i")
25 IGNORECASE = 1
26 else if (c == "l")
27 filenames_only++
28 else if (c == "e")
29 pattern = Optarg
30 else
31 usage()
32 }
33 if (pattern == "")
34 pattern = ARGV[Optind++]
35
36 for (i = 1; i < Optind; i++)
37 ARGV[i] = ""
38 if (Optind >= ARGC) {
39 ARGV[1] = "-"
40 ARGC = 2
41 } else if (ARGC - Optind > 1)
42 do_filenames++
43
44# if (IGNORECASE)
45# pattern = tolower(pattern)
46}
47#{
48# if (IGNORECASE)
49# $0 = tolower($0)
50#}
51function beginfile(junk)
52{
53 fcount = 0
54}
55function endfile(file)
56{
57 if (! no_print && count_only)
58 if (do_filenames)
59 print file ":" fcount
60 else
61 print fcount
62
63 total += fcount
64}
65{
66 matches = ($0 ~ pattern)
67 if (invert)
68 matches = ! matches
69
70 fcount += matches # 1 or 0
71
72 if (! matches)
73 next
74
75 if (! count_only) {
76 if (no_print)
77 nextfile
78
79 if (filenames_only) {
80 print FILENAME
81 nextfile
82 }
83
84 if (do_filenames)
85 print FILENAME ":" $0
86 else
87 print
88 }
89}
90END \
91{
92 if (total == 0)
93 exit 1
94 exit 0
95}
96function usage( e)
97{
98 e = "Usage: egrep [-csvil] [-e pat] [files ...]"
99 e = e "\n\tegrep [-csvil] pat [files ...]"
100 print e > "/dev/stderr"
101 exit 1
102}
Note: See TracBrowser for help on using the repository browser.