Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Linux - Regex
2.1.
Metacharacters involved in Regex
3.
Linux Regex with grep
3.1.
Print lines matching a pattern
3.2.
Searching any string
3.3.
Repeating pattern
3.4.
Match the start of a String
3.5.
Match the end of a string
3.6.
Searching for the string as a word
3.7.
Searching one or more patterns
4.
Linux Regex with Rename
4.1.
Global Replacement
4.2.
Case insensitive replacement
5.
Linux Regex with Sed
5.1.
White Space
5.2.
Optional Occurrence
5.3.
Exactly n Times Occurrence
6.
Frequently asked questions
6.1.
What is piping in Linux?
6.2.
How to replace all occurrences of pattern1 in the given file with pattern2?
6.3.
What is Linux Regex?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux Regex

Author Sonu Deo
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Many tasks that seem difficult to be performed in basic environments can be easily done in Linux environments. For example, changing the file format for many files is a very complicated task in a normal system, but it can be done with just one command in Linux.

Introduction to Linux Regex

In this article, we are going to look at how searching any pattern can be made easy with the help of Linux regex (regular expression), in which we don’t have to write the whole command; instead, we can specify what we are searching for with the help of regex.

Linux - Regex

Linux regular expressions are special characters that help search for data or find complex matching patterns in any file. These are used in many Linux programs like grep, bash, rename, etc.

Depending on the syntax of regular expressions, we divide them into three categories:

  • BRE: Basic Regular Expressions
  • ERE: Extended Regular Expressions
  • PCRE: Per-Compatible Regular Expressions

 

On the basis of the program or the tool, we can use one or more of these versions.

Metacharacters involved in Regex

A metacharacter is a character that has some special meaning during pattern processing. We use metacharacter in regular expressions to define the search conditions and any text manipulations.

A regular expression can have one or more repeating metacharacters. Some of the metacharacter in Linux Regex are listed below in the table with their meaning.

Metacharacters involved in Regex
Metacharacters involved in Regex
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Linux Regex with grep

GREP provides the following options to use Linux regex

  • -E: String is read as ERE.
  • -G: String is read as BRE.
  • -P: String is read as PCRE.
  • -F: String is read literally.

 

Before moving ahead to Linux commands, let’s first discuss a few things:

  • ERE, BRE, and PCRE are libraries with different meanings for different syntaxes. Execution of the same expression can be different based on the type of library used. For example, consider the characters ‘+’, ‘?’, and ‘{}’ in the case of BRE. These characters don’t have any special meaning until prefixed with a backslash ‘/’, but in the case of ERE, these characters have special meanings until prefixed with a ‘/’
     
  • While executing commands of Linux, if you encounter any problem with the syntax or options, you can use command_name —-help to get help about everything for that command.
     
  • We use –-color for highlighting the pattern in our search.
     
  • We use cat <FileName> to see the content inside that file.

Print lines matching a pattern

The grep command will search for lines in the files matching the specified pattern and print them. Let’s look at the commands below:

Syntax:

Grep <pattern> <FileName>


Command:

grep --color c coding_ninjas.txt
grep --color r coding_ninjas.txt
lines matching the string

Searching any string

We can also search for any string using the same syntax just use the pattern string in place of the character in the previous command. Let’s look at the commands below:

Syntax:

Grep <pattern> <FileName>


Command:

grep --color cod coding_ninjas.txt
search for any string

Repeating pattern

Searching for a repeating pattern, that if it occurs zero, one or more times.

‘pattern*’ denotes all strings with zero or more occurrences of pattern.

Pattern+’ denotes all strings with one or more occurrences of pattern.

Let’s look at the commands below:

Syntax:

grep <‘pattern*’> <FileName>
grep <’pattern+’> <FileName>


Command:

grep --color -E '1*' coding_ninjas.txt
grep --color -E '1+' coding_ninjas.txt
repeating pattern

We can see that in the first case, lines having zero or more occurrences of pattern got printed. But in the second case, only lines having one or more occurrences of pattern got printed.

Match the start of a String

Using grep, we can also search for lines starting with the specified pattern. For this, we use the caret sign (^). Let’s look at the commands below:

Syntax:

grep ^<pattern> <FileName>


Command:

grep --color ^00 coding_ninjas.txt
match the start of string

As we can see, only lines starting with the given pattern got printed.

Match the end of a string

For matching the end, we use the dollar sign ($). Let’s look at the commands below:

Syntax:

grep <pattern>$ <FileName>


Command:

grep --color 01$ coding_ninjas.txt
match the end of string

Searching for the string as a word

We can find the given pattern as a separate word in the file using the -w option. Let’s look at the commands below:

Syntax:

grep -w <pattern> <FileName>


Command:

grep --color -w code coding_ninjas.txt
string as a word

Searching one or more patterns

We can search for one or more patterns in the same file using a single command.

Here we use pipe (|) to signify that one or the other. Let’s look at the commands below:

Syntax:

grep <option> <'pattern2|pattern2> <FileName>


Command:

grep --color -E 'de|n' coding_ninjas.txt
searching for multiple patterns

 

Here we got each line having either pattern1 or pattern2. We can use the same command in the case of BRE, but in the case of PCRE, we have to use a backslash (\) before the or (|) operator like:

grep --color -P 'de\|n' coding_ninjas.txt

Linux Regex with Rename

We use rename command in Linux to search for a pattern and replace it with some new string. Let’s look at the commands below:

Syntax:

rename 's/string/other string/'


Command:

rename 's\text\txt\' *
rename

Here, all file names having text got replaced by txt.

Global Replacement

In the above-mentioned type of replacement, only the first occurrence of the pattern in the file name gets replaced but we can replace all the occurrences using global replacement. Let’s look at the commands below:

Syntax:

rename 's/string/other string/g'


Command:

rename '\s\txt\text\g' *
global replacement

Here, both the occurrences of the pattern in atxt.txt got replaced.

Case insensitive replacement

It is the method of replacing all the matching strings, irrespective of their casing, with some new string. Let’s look at the commands below:

Syntax:

rename 's/string/other string/i'


Command:

rename '\s\text\txt\i' *
case insensitive replacement

All the first occurrences, irrespective of their casing, got replaced with the specified string.

Linux Regex with Sed

The sed command is used for stream editing, like editing a whole text file. Let’s look at the commands below:

Command:

echo coding ninjas | sed 's/ing/er/'
altering in the stream

Here, we used echo to write any text and | operator for piping (check FAQs) then replaced all the ing with er.

We can edit and replace some strings in the whole file using the sed command using the interactive editor option (i). Let’s look at the commands below:

Syntax:

sed -i <'s/pattern/newstring/'> <FileName>


Command:

sed -i 's\ninjas\expert\' coding_ninjas.txt
altering in file

White Space

We can replace a tab space (\t) with a space (\s) using Linux regex and sed command.

Command:

echo -e 'coding\tninjas' | sed 's/\s/ /g'
white space

We replaced the tab space with a single space, as seen in the image above.

Optional Occurrence

We can replace any string in the file with its last character being optional, i.e., if we find a match with the pattern in the file and its last character is missing, it also will be replaced with the specified new string.

Let’s look at the commands below:

Command:

cat coding_ninjas.txt | sed 's/eee\?/E/'
optional occurrence

In the given command, we printed each line with an optional occurrence of the given pattern after replacing them with the specified string.

Exactly n Times Occurrence

We can replace the matching string if it occurs exactly n times. For example, we can specify for any value of n, and only the n times occurrence of a given pattern will be replaced by the specified new string. Let’s look at the following command:

Command:

Cat coding_ninjas.txt | sed ‘s/e\{3\}/E/’
n times occurrence

Here, all three times occurrences of ‘e’ get replaced with ‘E’.

Frequently asked questions

What is piping in Linux?

The Unix/Linux systems allow the stdout of a command to be connected to the stdin of another command. You can make it do so by using the pipe character ‘|’.

Check the image below here; sort test.txt is given as input to uniq command.

piping

How to replace all occurrences of pattern1 in the given file with pattern2?

We can do this type of change using the sed command. Its syntax will be as:

sed -i <'s/pattern/newstring/'> <FileName>

What is Linux Regex?

Linux regular expressions are special characters that help in searching for some data or finding complex matching patterns in any file. These are used in many Linux programs like grep, bash, rename, etc.

Conclusion

In this article, we discussed Linux Regex (regular expressions), which helps in searching for given patterns and doing operations on them. Different types of operations involve renaming file names and replacing or displaying the content of files. For this, we explored the use of Linux regex in different commands like rename, sed, and grep.

 

If you found this blog interesting, please visit these articles:


Visit our website to read more such blogs. Make sure you enroll in the courses we provide, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations.

Live masterclass