Linux grep Regular ExpressionsThe grep tool has the following options to use regular expressions:
Print Lines Matching A PatternThe grep command will search for line that matches the specified pattern. Syntax: Example: Look at the above snapshot, all the matching pattern lines are displayed and pattern is highlighted. Concatenating CharactersIf a pattern is of concatenating characters then it has to be matched as it is, for the line to be displayed. Example: Look at the above snapshot, lines matching exactly the specified patterns are displayed. One Or The OtherHere pipe (|) symbol is used as OR to signify one or the other. All the three versions are shown. Options -E and -P syntax are same but -G syntax uses (\). Syntax: Example: Look at the above snapshot, either pattern 'j' or 'g' should be matched to display the lines. One Or More / Zero Or MoreThe * signifies zero or more times occurence of a pattern and + signifies one or more times occurence. Syntax: Example: Look at the above snapshot, * character displays zero or more times occurence of pattern '1'. But + character displays one or more times occurence. Match The End Of A StringTo match the end of a string we use $ sign. Syntax: Example: Look at the above snapshot, lines are displayed matching the end of a string. Match The Start Of A StringTo match the start or beginning of a file we use caret sign (^). Syntax: Example: Look at the above snapshot, lines are displayed matching the start or beginning of a string. Separating WordsSyntax: Example: Look at the above snapshot, by giving command "grep some file all the lines matching to the word 'some' are displayed. But by giving command "grep '\bsome\b' file" only lines matching single word 'some' are displayed. Note: This can also be done with the help of -w option. Syntax: Example: Look at the above snapshot, command "grep -w some file" displays the same result as \b character.
Next TopicLinux rename Regular Expression
|