1 / 9

UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1)

UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1). Gilbert Detillieux April 13, 2010 MUUG Meeting. What’s in a name?. grep : g lobal r egular e xpression p rint Comes from UNIX editor command: g/ re /p egrep : extended grep Extended (or full ) regular expressions

raoul
Download Presentation

UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIX CommandsRTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010MUUG Meeting

  2. What’s in a name?... • grep: global regular expression print • Comes from UNIX editor command: g/re/p • egrep: extendedgrep • Extended (or full) regular expressions • fgrep: fastgrep or fixed-string grep • Matches fixed or literal strings • Historically, much faster than grep/egrep

  3. 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 thosespecial characters literally

  4. 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: “*$”

  5. 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

  6. Full Regular Expressions (cont.) • Sub-expressions enclosed in “(…)”: • “(the)?end” matches “end” or “the end” • “(pretty)*please” • Alternatives separated by “|”… • “(this|that|theother)” • “(this|that|)” • And many more extensions (possibly)…

  7. grep(1) Man Page • NAME • grep, egrep, fgrep – print lines matching a pattern • SYNOPSIS • grep [-cilnqv]... PATTERN [ FILE … ] • grep [-cilnqv]... [ -ePATTERN | -f FILE ][ FILE … ]

  8. 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 • -ePATTERN makes pattern explicit,even if starting with “-” • -f FILE takes list of patterns from file, one per line

  9. Questions?

More Related