Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Google Cloud

Regex in Linux: Regular Expression Syntax for Beginners

by Cloudbooklet
4 years ago
in Google Cloud
Regex For Linux – Regular Expression Syntax For Beginners
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

Regex in Linux, regular expressions are similar to the wildcards which allow us to create a pattern to perform a specific search in data. Regular expressions are lot more powerful and also known as “regex” or “regexp“. The commands in Linux that are commonly used with regex are tr, sed, vi and grep. Basic Regular […]

ADVERTISEMENT

Regex in Linux, regular expressions are similar to the wildcards which allow us to create a pattern to perform a specific search in data. Regular expressions are lot more powerful and also known as “regex” or “regexp“.

The commands in Linux that are commonly used with regex are tr, sed, vi and grep.

Basic Regular Expressions

Here are listed some of the basic regular expressions used on Linux.

ADVERTISEMENT
ExpressionDescription
^Matches the beginning of the string
$Matches the end of the string
.Matches any single character except “n“
*Matches up zero or more times the preceding character
\Preserve the meaning of the character by escaping it
()Group the characters
?Match one character

Examples

To search a file with a specific letter you can use the following command.

You might also like

How To Setup Ssh Keys On Ubuntu

How to Setup SSH Keys on Ubuntu 20.04

4 months ago
Draggan Ai Editing Tool Install And Use Draggan Photo Editor

DragGAN AI Editing Tool Install and Use DragGAN Photo Editor

4 months ago
grep a content.txt

This command searches for the occurrence of “a” in the content.txt file and outputs it.

To search for a line that starts with a specific character or word you can use this “^” expression.

ADVERTISEMENT
grep ^d content.txt

This command outputs all lines that is starting with “d”.

To search for a line that ends with a specific character or word you can use this “$” expression.

ADVERTISEMENT
grep g$ content.txt

This command outputs all lines that is ending with “g”.

To search for empty line you can use the following command.

ADVERTISEMENT
grep '^$' content.txt

Interval Regular Expressions

The interval regular expressions are used to match the number of times the characters appear in a string.

ExpressionDescription
{n}Matches the character appearing “n” number of times.
{n,m}Matches the character appearing “n” number of times but not more than “m“
{n,}Matches only if there are more than “n” number of characters

Example

grep -E n{2} content.txt

This command outputs the line that contains 2 occurrences of “n”.

ADVERTISEMENT
Output
Running

Extended Regular Expressions

Extended regular expressions are combination of two or more expressions as listed below.

ExpressionDescription
\+Match one or more occurrences of the previous character.
\?Matches zero or one occurrence of the previous character.

Example

To filter out lines where character ‘o’ precedes character ‘m’ you can use this kind of expression.

grep "o\+m" content.txt

This command outputs something like this.

Output
Zombies

These are some of the common regular expressions used in Linux.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

Conclusion

Now you have learned how to create a regular expression for Linux.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Tags: CentOSDebianUbuntu 18.04
ShareTweetSendShare
Cloudbooklet

Cloudbooklet

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Set Up Deep Learning With Nvidia, Cuda, Cudnn On Ubuntu

How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04

7 months ago
How To Install Or Upgrade Php 8.2 On Ubuntu 22.04

How to Install or Upgrade PHP 8.2 on Ubuntu 22.04

9 months ago
How To Change Timezone On Ubuntu 22.04

How to Change Timezone on Ubuntu 22.04

1 year ago
How To Install Ansible On Ubuntu 22.04

How to Install Ansible on Ubuntu 22.04

1 year ago

Follow Us

Trending Articles

Amazon Prime

Amazon Prime Big Deal Days 2023: Best Deals

September 21, 2023

5 Free AI Soulmate Maker: Create Your Perfect Match

10 Best AI Prompts for Writers to Improve Website SEO

5 Best TikTok Private Account Viewer in 2023

WhatsApp Business Gets New Features to Help your Businesses Grow

Microsoft Unveils New Disc-Less Xbox Series X with Lift-to-Wake Controller

Popular Articles

Microsoft Editor Vs Grammarly

Microsoft Editor vs Grammarly: Which is the Best Grammar Checker?

September 18, 2023

8 Best Tools for Website Malware Scanning Online Free

How to Use Adobe AI Audio Enhancer to Fix and Edit Your Recordings

8 Best AI GIF Generators of 2023

7 Best AI Girl Generators for Creating Realistic and Beautiful AI Girls

How to Clear Gmail Inbox with AI: Tips and Tricks

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.