unix commands rtfm: grep(1), egrep(1) & fgrep(1) gilbert detillieux april 13, 2010 muug meeting
TRANSCRIPT
![Page 1: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/1.jpg)
UNIX CommandsRTFM: grep(1), egrep(1) & fgrep(1)
Gilbert DetillieuxApril 13, 2010MUUG Meeting
![Page 2: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/2.jpg)
What’s in a name?...
grep: global regular expression printComes from UNIX editor command: g/re/p
egrep: extended grepExtended (or full) regular expressions
fgrep: fast grep or fixed-string grepMatches fixed or literal stringsHistorically, much faster than grep/egrep
![Page 3: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/3.jpg)
Limited Regular Expressions
A.k.a. “basic” regular expressions Alpha-numerics match literally So does some punctuation, unless special Similar to wildcards in file-name “globbing”
“.” matches any single character “*” matches any number of previous character
(including zero) Use “\.” (or “\*”) to match those
special characters literally
![Page 4: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/4.jpg)
Limited Regular Expressions (cont.)
Character classes:“[0-9]” matches any digit“[^A-Za-z]” matches anything but letters“[0-9-]” matches “-” (literally) or a digit“[][]” matches “]” or “[” (literally)
Anchors:“^” matches beginning of line: “^From ”“$” matches end of line: “ *$”
![Page 5: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/5.jpg)
Full Regular Expressions
A.k.a. “extended” regular expressions Additional repetition operators like “*”…
“?” matches zero or one instance of previous character or sub-expression
“+” matches one or more instances of previous character or sub-expression
![Page 6: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/6.jpg)
Full Regular Expressions (cont.)
Sub-expressions enclosed in “(…)”:“(the )?end” matches “end” or “the end”“(pretty )*please”
Alternatives separated by “|”…“(this|that|the other)”“(this|that|)”
And many more extensions (possibly)…
![Page 7: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/7.jpg)
grep(1) Man Page
NAMEgrep, egrep, fgrep – print lines matching a
pattern SYNOPSIS
grep [-cilnqv]... PATTERN [ FILE … ] grep [-cilnqv]... [ -e PATTERN | -f FILE ]
[ FILE … ]
![Page 8: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/8.jpg)
grep(1) Man Page (cont.)
OPTIONS -c only prints a count of matching lines -i ignores letter case distinctions -l only prints list of names of files with matches -n prefixes line numbers to matching lines -q is quiet, returning only appropriate exit status -v selects variant or non-matching lines -e PATTERN makes pattern explicit,
even if starting with “-” -f FILE takes list of patterns from file, one per line
![Page 9: UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697bfd31a28abf838cac037/html5/thumbnails/9.jpg)
Questions?