infoLinux

grep command in Linux – Regular Expressions & Options

grep command is used to search for string in a source. The source can be piped to it. Or it can read a file as source. Linux grep command can search with regular expression. Additionally, it has multiple options for specific purpose.

grep man page – summary

A simplest example of grep could be the following:

grep simple command

So you can see the basic usage is

$ grep <string_to_search> <file_name>

In the first command, cat content.txt we’ve shown the all contents in the file. But in the second command grep “Linux” content.txt, we’re only searching for the “Linux” word in the content.

We can also use grep using pipes. For example, if we want to find a process which contains the name java, we could do:

$ ps aux | grep ‘java’

The shell will first generate the output for ps aux, then the output will be piped to grep ‘java’, and finally we will only see the lines containing the ‘java’ string.

Synopsis of grep Command:

grep [OPTION…] PATTERNS [FILE…]
grep [OPTION…] -e PATTERNS … [FILE…]
grep [OPTION…] -f PATTERN_FILE … [FILE…]

The command grep tool has the following options to use regular expressions:

  • -E, –extended-regexp
    Interpret PATTERNS as extended regular (ERE)
  • -G, –basic-regexp
    Interpret PATTERNS as basic regular expressions. This is the default. (BRE)
  • -P, –perl-regexp
    Interpret PATTERNS as Perl-compatible regular expressions (PCREs).
  • -F, –fixed-strings
    Interpret PATTERNS as fixed strings, not regular expressions

Pipe Symbol: One OR The Other

Here pipe (|) symbol is used as OR to signify one or the other. BRE and PCRE supports the pipe by default. For ERE we need to use \| instead of |

Syntax:

$ grep <options> <pattern1|pattern2> <filename>

Example :

$ grep -E ‘OS|Ubuntu’ text.txt  $ grep -P ‘OS|Ubuntu’ text.txt  $ grep -G ‘OS\|Ubuntu’ text.txt

Asterisk and Plus sign: One Or More / Zero Or More

( * ) signifies zero or more times occurrence of a pattern, and (+) signifies one or more times occurrence.

Syntax:

$ grep <option> <‘pattern*’> <fileName>

For Example :

$ grep -E ‘1*’ list  $ grep -E ‘1+’ list

Match The End Of A String

To match the end of a string we use sign.

Syntax:

$ grep <pattern>$ <fileName>

For Example:

$ grep Kernel$ text.txt  $ grep Linux$ text.txt

Match The Start Of A String

To match the start or beginning of a file we use caret sign (^).

Syntax:

$ grep ^<pattern> <filename>

Example:

$ grep ^T text.txt

To Match Line that contains only foo

$ grep ^foo$ text.txt

To Match Blank Lines

$ grep ^$ test.txt

Matching Set of Characters using Third Brackets

The hyphen (-) matches a range between characters. For example:

$ grep ‘[0-9]me[a-z]’ test.txt

will match 1met, 2meu etc. If we want to match two letters, or two digits, we could do

$ grep ‘[0-9][0-9]me[a-z][a-z]’ test.txt

Character Classes:

  • [[:alnum:]] – Alphanumeric Characters
  • [[:alpha:]] – Alphabetic Characters
  • [[:blank:]] – Blank Characters like space or tab
  • [[:digit:]] – Numerical Digits: 0 to 9
  • [[:lower:]] – Lowercase Characters
  • [[:upper:]] – Uppercase Charcaters
  • [[:space:]] – tab, newline, vertical tab, space for feed, carriage return etc

For Example a pattern for matching IP addresses will be

[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}

For Example:

grep -E “[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}” text.txt

The dot(.) represents any character in RE.

For example:

$ grep -E ‘W..t’ text.txt

Will match any words which have W first then any two letters and then t. Example: What, WBUt, W09t etc

For complete man page reference, see grep man page.

Leave a Reply

Your email address will not be published. Required fields are marked *