Using Regular Expressions in Grep

  • 398 views
Uploaded on

Using regular expressions and logic with GREP and Linux

Using regular expressions and logic with GREP and Linux

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
398
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
9
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Dan MorrillHighline Community College April 02 - 2013
  • 2.  Superheroes.txt A linux computer Grep (already on your linux box)
  • 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.  -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.  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.  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.  . (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.  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.  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