1 / 14

Demystifying grep: A Comprehensive Guide

Understand grep, egrep, fgrep and their functionalities in Unix. Learn how to use patterns, options, and metacharacters effectively with practical examples. Uncover the origins and workings of these powerful search tools.

ceballos
Download Presentation

Demystifying grep: A Comprehensive Guide

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 Talk #2 grep/egrep/fgrep

  2. grep • Where does the name come from? • :g/RE/p • globally regular expression print • How it works? • grep looks inside file(s) and returns any line that contains the string or expression • prints lines matching a pattern to STDOUT

  3. grep examples • grep Pattern • grep Pattern filename • Ex. grep ‘permission’ thisFile • grep pattern file1 file2 • Ex. grep ‘permission’ file1 file2 • file1: • file2: • If grep cannot find a line in any of the specified files that contain the requested pattern, no output is produced.

  4. grep Exit Status • $? is set to 0 • if grep finds something • $? is set to 1 • if grep finds nothing • $? is set to 2 • if one of the input file cannot be found. • sed and awk do not use the exit status to indicated the success or failure of locating a pattern. They report failure only if there is a syntax error in a command.

  5. grep continue • The general format: • grep [options] PATTERN [FILE…] • Reading from files • grep re * • Reading from STDIN • grep ‘pattern’ • cat file | grep ‘pattern’ • ls –l | grep ‘Jan’

  6. More examples A=‘This is it’ echo $A | grep ‘This’ A=`grep ‘tborrelli’ /etc/passwd` if [[ -z $A ]] ; then echo “tborrelli Not found” else echo “tborrelli found” fi

  7. grep Options • grep • -i: ignore case • grep –i unix filename • -n: list line numbers along with the matching line • [File:]lineNumber:theLine • -v: invert match • grep –v ‘#’ /etc/hosts • Produces a list of all the lines in /etc/hosts that do not contain the # character. • ps –ef | grep tborrelli | grep –v grep

  8. grep Options cont’ • -l: only listing the filenames • grep –l delete projects/* • Pqops.c • Pqops.h • Scheduler.c • -w: matches the whole word • grep –w wordonly FILE • grep –w north datafile • -c: Print the number of lines where the pattern was found • grep –c ‘^no[a-z]*’ datafile • -A [B] num: Print num of lines after [before] match lines • grep –A 1 ‘^south[a-z]’ datafile

  9. The grep family • grep • grep ‘^[0-9][0-9]*$’ • Try grep ‘^[0-9]+$’ • Try grep ‘^[0-9]\+$’ • egrep (grep –E) • Extended/Enhanced grep with more RE metacharacters • egrep ‘^[0-9]+$’ • fgrep (grep –F) • Fixed/fast grep, treats all characters as literals • fgrep ‘^[0-9]+$’ • rgrep (grep –r/R) • Recursive grep

  10. fgrep • Special case: fgrep fgrep STRING file1 file2 … • Same as grep -F STRING file1 file2 … • The STRING is a fixed string, not a REGEXP • All metacharacters are disabled – i.e. they are treated as themselves

  11. egrep • Special case: egrep egrep REGEXP file1 file2 … • Same as egrep REGEXP file1 file2 … • The meta characters ?, +, {, |, (, and ) have their special meanings • In grep (as opposed to egrep) these have no special meaning unless escaped with a backslash \

  12. egrep • Metacharacter +, ?, a|b, ( ) • Examples (copy the datafile from /home/fac/pub) • egrep ‘NW|EA’ datafile • egrep ‘3+’ datafile • egrep ‘2\.?[0-9]’ datefile • egrep ‘(no)+’ datafile • egrep ‘S(h|u)’ datafile • egrep ‘Sh|u’ datafile • egrep ‘[[:space:]]\.[[:digit:]][[:space:]]’ datafile

  13. egrep (Con’t) • egrep ‘^$’ file • egrep ‘fun\.$’ file • egrep ‘[A-Z]…[0-9]’ file • egrep –v ‘[tT]est’ file • egrep –i ‘sam’ file • egrep –l ‘Dear Boss’ * • egrep –n “$name” file

  14. The grep family (Con’t) • GNU grep • grep • egrep or grep –E • fgrep or grep –F • More metacharacters • \w same as [a-zA-Z0-9_] • \W same as [^a-zA-Z0-9_]

More Related