Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linux
regex

What are Regular Expressions in Unix / Linux?

Sarvech Qadir

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.

widget

Operations

Below are some basic operations performed in Linux regular expressions.

  1. .: This symbol replaces any character.

  2. ^: This symbol checks if a certain character matches start of the string.

  3. : This symbol checks if a certain character matches end of the string.

  4. *: This symbol matches zero (or more) occurrences of a character.

  5. \: This symbol represents special characters.

  6. (): This symbol groups several regular expressions.

  7. ?: This symbol matches up exactly one character.

Example

Let’s look at a few terminal examples:

Imagine you have a file, greetings.txt:

// 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


RELATED TAGS

linux
regex

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring