Dan MorrillHighline Community College       April 02 - 2013
 Superheroes.txt A linux computer Grep (already on your linux box)
 Grep searches the named input FILEs (or standard  input if no files are named, or the file name - is given)  for lines c...
 -E – extended-regexp (interpret a pattern (using a    regular expression))   -G – basic-regexp (interpret a pattern as ...
 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 ...
 grep –i black superheroes.txt   You should see a list of people who have the word man    in their names grep –i cat su...
 . (period) – match any single character ^ - match the empty string at the top of the line $ - match the empty string a...
 X? – match no or one occurance of the captial letter X X* - match zero or more captial x’s X+ - match one or more capt...
 grep –E ‘^Bat’ superheroes.txt   Matches names that start with Bat (note the cap)     What would I use to make it not ...
Using Regular Expressions in Grep
Upcoming SlideShare
Loading in...5
×

Using Regular Expressions in Grep

473

Published on

Using regular expressions and logic with GREP and Linux

Published in: Education, Technology, Lifestyle
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
473
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Using Regular Expressions in Grep"

  1. 1. Dan MorrillHighline Community College April 02 - 2013
  2. 2.  Superheroes.txt A linux computer Grep (already on your linux box)
  3. 3.  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.
  4. 4.  -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
  5. 5.  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.
  6. 6.  grep –i black superheroes.txt  You should see a list of people who have the word man in their names grep –i cat superheroes.txt  You should see a list of people who have the word cat in their names grep –i spider superheroes.txt  You should see a list of people who have the word spider in their names grep -v -i spider heroes.txt  You should see a list of people without the word spider in their names
  7. 7.  . (period) – match any single character ^ - match the empty string at the top of the line $ - match the empty string at the bottom of the line A – match an uppercase A a – 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
  8. 8.  X? – match no or one occurance of the captial letter X X* - match zero or more captial x’s X+ - 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
  9. 9.  grep –E ‘^Bat’ superheroes.txt  Matches 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.txt  Matches all bat, Bat, cat, Cat in the file grep –i –E ‘^(bat|cat)’ superheroes.txt  Matches 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

×