A Regular Expression is a sequence of characters that we can use for pattern matching, or for searching and replacing text.
This expression is not the property of just one particular programming language. It is supported by multiple languages.
Regular Expressions in Linux refer to some special characters. We use these characters to search data and match certain complex patterns.
Below are some basic operations performed in Linux regular expressions.
.: This symbol replaces any character.
^: This symbol checks if a certain character matches start of the string.
＄: This symbol checks if a certain character matches end of the string.
*: This symbol matches zero (or more) occurrences of a character.
\: This symbol represents special characters.
(): This symbol groups several regular expressions.
?: This symbol matches up exactly one character.
Let’s look at a few terminal examples:
Imagine you have a file,
// greetings.txt Hello Hi Hey Good Morning Good night Bye What's up Yo
Now let’s use these basic regex expressions and play with the contents of this file:
>> cat greetings.txt // cat command is used extract the content of a file. Hello Hi Hey Good Morning Good night Bye What's up Yo // grep is used for search in linux. // pipe "|" is a redirection that the output of command before pipe will be used in the command after the pipe. // print all the word containing letter 'a'. >> cat greetings.txt | grep a What's up // print all the word containing letter 'e'. >> cat greetings.txt | grep e Hello Hey Bye // you can also give a and e collectively as a list. >> cat addr1.txt | grep [a,e] Hello Hey Bye What's up // print all the word starting with letter 'H'. >> cat greetings.txt | grep ^H Hello Hi Hey // print all the words ending with letter 'o'. >> cat greetings.txt | grep o$ Hello Yo // print all the words containing the letter 'a' and one letter from the left. >> cat greetings.txt | grep .a What's up // print all the words containing the letter 'e' and highlights one letter from the left since the '.' is on left of 'e'. >> cat greetings.txt | grep .e Hello Hey Bye // print zero or more occurences of "at". >> cat addr1.txt | grep "a\?t" Good night What's up
View all Courses