Introduction:
Regular expressions are used to search and manipulate the text, based on the patterns. Grep command is used to search for a specific string in a file. Also use regular expressions with grep command when you want to search for a text containing a particular pattern.
Regular Expressions:
Regular Expressions are special characters which help search data, matching complex patterns. A regular expression, often shortened to “regex” or “regexp”. Regular Expression enhance the ability to meaningfully process text content, especially when combined with other commands.
Patterns that exactly specify the characters to be matched are called “literals” because they match the pattern literally, character-for-character.
The period character and the special meta-character (.) is used in regular expressions to mean that any single character can exist at the specified location.
Anchors are special characters that specify where in the line a match must occur to be valid.
Use the “^” anchor before the literal string. Similarly, the “$” anchor can be used after a string to indicate that the match will only be valid if it occurs at the every end of a line.
One of the most commonly used meta-characters is the “*”, which means “repeat the previous character or expression zero or more times”.
We can escape characters by using the backlash character (\) before the character that would normally have a special meaning.
One of the easiest and most useful abilities that extended regular expressions open up is the ability to group expressions together to manipulate. Group expressions together using parentheses.
A bracket expression is a list of characters enclosed by
. It matches any single character in that list
. | Match a single character of any value |
^ | Anchor symbol to match a starting at the beginning of line |
$ | Dollar symbol to match end of the line |
* | Matches up zero or more times the preceding character |
\ | Represent special characters |
() | Groups regular expressions |
? | Matches up exactly one character |
[] | Range of character |
^$ | Count of empty lines |
Grep command:
The grep command which stands for “Graphical Regular Expression Print,” The grep command is used to search text or searches the given file for lines containing a match to the given string or words. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Grep is a powerful file pattern searcher in Linux.
Install grep package [root@localhost ~]#yum install grep |
Search the given string in specified file [root@localhost ~]#grep “linux” file |
Case insensitive search [root@localhost ~]#grep –I “linux” file |
Display don’t contain a specified string [root@localhost ~]#grep –v linux file |
Regular expression anything [root@localhost ~]#grep “fast.*host” file |
Display the line numbers contains matches [root@localhost ~]#grep –n “word” file |
Highlighting matched search [root@localhost ~]#grep –color “linux” file |
Display lines starts with root word [root@localhost ~]#grep ^root /etc/passwd |
Display line ends with bash word [root@localhost ~]#grep bash$ /etc/passwd |
Search pattern recursively [root@localhost ~]#grep –r linux /etc |
Counting the lines when word match [root@localhost ~]#grep –c ‘test’ file |
Counting any single character between c and t [root@localhost ~]#grep c.t /usr/share/dict/words |
Display 2 lines after the regex match [root@localhost ~]#grep –A 2 ‘test’ file |
Display 2 lines before the regex match [root@localhost ~]#grep –B 2 ‘test’ file |
Display 2 lines before and after the regex match [root@localhost ~]#grep –c 2 ‘test’ file |
Contains any one character found in bracket [root@localhost ~]# grep c[aou]t /usr/share/dict/words |
Contains either string in the file [root@localhost ~]# grep -e cat -e tele /usr/share/dict/words |
Find every line that begins with a capital letter [root@localhost ~]#grep “^[A-Z]” file |