Using Regular Expressions in Grep

10
Dan Morrill Highline Community College April 02 - 2013 CIS 216 – Regular Expressions using Grep

description

Using regular expressions and logic with GREP and Linux

Transcript of Using Regular Expressions in Grep

Page 1: Using Regular Expressions in Grep

Dan Morrill

Highline Community College

April 02 - 2013

CIS 216 – Regular Expressionsusing Grep

Page 2: Using Regular Expressions in Grep

Superheroes.txtA linux computerGrep (already on your linux box)

Files Needed

Page 3: Using Regular Expressions in Grep

Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available. Egrep is the same as grep -E. Fgrep is the same as grep -F.

What is Grep?

Page 4: Using Regular Expressions in Grep

-E – extended-regexp (interpret a pattern (using a regular expression))

-G – basic-regexp (interpret a pattern as a basic regular expression)

-i – ignore case (important, linux is case sensitive)

-n – print the line number the match was found on

-r (-R) – recursive – search all the files under a directory for the pattern

-v – invert match – show all lines that are not matching of the pattern

Important Grep Switches

Page 5: Using Regular Expressions in Grep

Grep is designed to search for data in a file or in a list (for example, when doing ps –ef |grep http)

If you want to search for a specific item in a text document you can also use grep to find what you are looking for

grep –i man superheroes.txt Will find everything that has the word “man” in

the file and push it to the screen for you to see.

How to parse data in a file using Grep

Page 6: Using Regular Expressions in Grep

grep –i black superheroes.txtYou should see a list of people who have the word

man in their namesgrep –i cat superheroes.txt

You should see a list of people who have the word cat in their names

grep –i spider superheroes.txtYou should see a list of people who have the word

spider in their namesgrep -v -i 'spider' heroes.txt

You should see a list of people without the word spider in their names

Let’s try it:

Page 7: Using Regular Expressions in Grep

. (period) – match any single character^ - match the empty string at the top of the line$ - match the empty string at the bottom of the lineA – match an uppercase Aa – match a lowercase a\d – match a digit (number)\D – match any non-number character (a-zA-Z)[A-E] – match any upper case A through E (A, B, C,

D, E)[^A-E] – match any upper case character but A

through E

Modifiers

Page 8: Using Regular Expressions in Grep

X? – match no or one occurance of the captial letter X

X* - match zero or more captial x’sX+ - match one or more captial x’s(abc|def)+ Match a sequence of at least one

abc and def, abc and def would both match

Modifiers

Page 9: Using Regular Expressions in Grep

grep –E ‘^Bat’ superheroes.txtMatches names that start with Bat (note the

cap)What would I use to make it not case sensitive?

grep -E '^(bat|Bat|cat|Cat)‘ superheroes.txtMatches all bat, Bat, cat, Cat in the file

grep –i –E ‘^(bat|cat)’ superheroes.txtMatches all bat and cat regardless of case

(similar to the second example without so much typing)

grep -i -E '[^b]at' superheroes.txt Excludes all lowercase b followed by at

Let’s try it:

Page 10: Using Regular Expressions in Grep

Questions?