Wednesday, December 5, 2012

Everything You Wanted to Know about Grep

How to run grep

Grep is a unix filter program. q.v.

Specifying the input data

Typically we specify the input data using a filename or a pipe:
grep regexp filename
cat filename | grep regexp

Specifying the grep instruction

The -f and -e options are common to grep, egrep, fgrep, sed and awk. This is the 'program' that grep executes - this is however just a regex match
grep -e -style doc.txt
grep -f pattern.txt searchhere.txt

Important grep options

# case insensitive search
grep -i word

# don't match
grep -v word

# Includes the name of the file before each line printed
grep -H *

# Supresses the name of the file before each line printed
grep -h *

# print line number of matched line
grep -n pattern filename

grep variations

Basic grep - grep or grep -G
Uses basic regular expressions only.

Extended grep - egrep or grep -E
Uses a full set of regular expressions.

Fixed string grep - fgrep or grep -F
Interprets the pattern as a string and not as a regular expression.

Perl-Style Regular Expressions -grep -P
Uses the heavy regular expressions features that Perl made popular

No comments:

Post a Comment