Lesson 9

Date: 3/26/2014
Linux shell scripting tutorial
Linux for Engineering and IT Applications


Introduction to GREP
grep is used to search files or standard input for lines containing required patterns.

We'll work with a text file, list.txt, containing the following text:

Check the inode list today
reboot the machine tomorrow
  Reboot it again in a week 
Call Tech support in case of emergency.
tel: 834 

Oop 0
Oops 1
Oopss 12
Oopsss 123
Oopssss  1234
End

To get the line containing string "inode" in file list.txt:
grep inode  list.txt

To get the line containing "inode lis " in file list.txt:
grep "inode lis " list.txt

It should give you nothing as there is no string " lis "

To search for the line containing "inode list" in all the files in current directory:
grep "inode list" *


Syntax of grep:
grep [options] regex [files]
where regex are regular expressions.

Using regular expressions
Regular expressions: Literals (plain text or literal text),
metacharacters (special meaning characters).
When you construct regular expressions, you use metacharacters and literals to specify three basic ideas about your input text: position anchors, groups, ranges and quantity modifiers.
Anchors: ^  -match at the beginning of a line
         $  -match at the end of a line
grep '^Call' list.txt 
grep '^  Reboot' list.txt
grep 'today$' list.txt
 

Count the number of empty lines:
grep -c '^$' list.txt

Display all lines containing only the word End by itself:
grep '^End$' list.txt

Groups and ranges: [abc]   -match any single character from a,b or c 
                   [a-e]   -match any single charcter from among the range a-e  
                   [^abc]  -inverse match, matches a single character not
                            among a,b, or c.
                   [^a-e]  -inverse match, matches a single character not from 
                            the range a-e
              \< word\>    -match word
            . (single dot) -match any single character among a new line
                  \        -turn off the special meaning of the character 
                            that follows
grep '[Rr]eboot' list.txt
grep '\<[Rr]eboot\>' list.txt

Display all lines from file list.txt which contain thre adjucent digits:
grep '[0-9][0-9][0-9]' list.txt

Display the lines with four or more characters in the line:
grep '....' list.txt

Display all non-blank lines from file list.txt:
grep '.' list.txt

Display all lines that contain a period:
grep '\.' list.txt

Modifiers:  *  -match zero or more instance of the preceding single character 
            ?  -match zero or one  instance of the preceding regex
                (implies 'grep -E' option).  
            +  -match one or more  instance of the preceding regex
                (implies 'grep -E' option).
       \{n,m\} -match a range of occurrences of the single character or regex
                 that precedes this construct; \{n\} matches n occurences;
                 \{n,\} matches at least n occurences.      
            |  -match either the regex specified before or after the
                vertical bar (implies 'grep -E' option).

Display all lines from list.txt that contain Oop, Oops, Oopss, and so on:
grep 'Oops*' list.txt
Display all lines from list.txt that contain Oops, Oopss, and so on:
grep 'Oopss*' list.txt
 

Display all lines from list.txt that contain two or more adjacent digits:
grep '[0-9][0-9][0-9]*' list.txt

Display all lines from list.txt that contain '3' or '34' number combination:
grep -E '34?' list.txt

Display all lines from list.txt containing at least one digit:
grep -E '[0-9]+' list.txt 

Display all lines from list.txt containing sss and ssss:
grep 's\{3,4\}' list.txt

Display all lines from list.txt containing any three, four or five digit numbers:
grep '\<[0-9]\{3,5\}\>' list.txt

Display all lines from list.txt containing "Reboot", "reboot" or "support" strings:
grep -E '[Rr]eboot|support' list.txt

Display all lines from list.txt containing any letter (no empty lines):
grep '[A-Za-z]' list.txt

Display all lines from list.txt containing any non alpha-numeric and space symbol:
grep '[^ 0-9A-Za-z]' list.txt

Display all lines from list.txt containing uppercase letter, followed by zero or more lowercase letters:
grep '[A-Z][a-z]' list.txt

Display all lines from list.txt containing 3 digit telephone number:
grep 'tel: [0-9]\{3\}' list.txt

Recommended tutorial on Regular expressions and grep


Take me to the Course Website