Last Updated : 30 Jan, 2019
Prerequisite:
grepBasic Regular Expression
Regular Expression provides an ability to match a "string of text" in a very flexible and concise manner. A "string of text" can be further defined as a single character, word, sentence or particular pattern of characters. Like the shell’s wild–cards which match similar filenames with a single expression, grep uses an expression of a different sort to match a group of similar patterns.
Examples
(a) [ ]: Matches any one of a set characters
$grep “New[abc]” filenameIt specifies the search pattern as :
Newa , Newb or Newc
$grep “[aA]g[ar][ar]wal” filenameIt specifies the search pattern as
Agarwal , Agaawal , Agrawal , Agrrwal agarwal , agaawal , agrawal , agrrwal
: Matches any one of a range characters
$grep “New[a-e]” filenameIt specifies the search pattern as
Newa , Newb or Newc , Newd, Newe
$grep “New[0-9][a-z]” filenameIt specifies the search pattern as: New followed by a number and then an alphabet.
New0d, New4f etc
: The pattern following it must occur at the beginning of each line
$grep “^san” filenameSearch lines beginning with san. It specifies the search pattern as
sanjeev ,sanjay, sanrit , sanchit , sandeep etc.
$ls –l |grep “^d”Display list of directories only
$ls –l |grep “^-”Display list of regular files only
: The pattern must not contain any character in the set specified
$grep “New[^a-c]” filenameIt specifies the pattern containing the word “New” followed by any character other than an ‘a’,’b’, or ‘c’
$grep “^[^a-z A-Z]” filenameSearch lines beginning with an non-alphabetic character
: The pattern preceding it must occur at the end of each line
$ grep "vedik$" file.txt(f) Use . (dot)
: Matches any one character
$ grep "..vik" file.txt $ grep "7..9$" file.txt(g) Use \ (backslash)
: Ignores the special meaning of the character following it
$ grep "New\.\[abc\]" file.txtIt specifies the search pattern as New.[abc]
$ grep "S\.K\.Kumar" file.txtIt specifies the search pattern as
S.K.Kumar
: zero or more occurrences of the previous character
$ grep "[aA]gg*[ar][ar]wal" file.txt(i) Use (dot).*
: Nothing or any numbers of characters.
$ grep "S.*Kumar" file.txt
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4