|
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:
To get the line containing "inode lis " in file 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:
Syntax of grep:
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
|
Count the number of empty lines:
Display all lines containing only the word End by itself:
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
|
Display all lines from file list.txt which contain thre adjucent digits:
Display the lines with four or more characters in the line:
Display all non-blank lines from file list.txt:
Display all lines that contain a period:
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:
Display all lines from list.txt that contain Oops, Oopss, and so on:
Display all lines from list.txt that contain two or more adjacent digits:
Display all lines from list.txt that contain '3' or '34' number combination:
Display all lines from list.txt containing at least one digit:
Display all lines from list.txt containing sss and ssss:
Display all lines from list.txt containing any three, four or five digit
numbers:
Display all lines from list.txt containing "Reboot", "reboot" or "support"
strings:
Display all lines from list.txt containing any letter (no empty lines):
Display all lines from list.txt containing any non alpha-numeric and space symbol:
Display all lines from list.txt containing uppercase letter, followed by zero
or more lowercase letters:
Display all lines from list.txt containing 3 digit telephone number:
Recommended tutorial on Regular expressions and grep
|
|