5 Basic Regular Expressions for Efficient Text Manipulation in Linux Terminal

5 Basic Regular Expressions for Efficient Text Manipulation in Linux Terminal

Are you struggling with finding patterns in your Linux terminal? Regular expressions (regex) and grep are powerful tools that can help you search and manipulate text. In this article, we’ll dive into the 5 regular expressions with grep and provide examples that you can use to practice.

Matching a word:

The most basic regular expression is matching a specific word or phrase. The syntax for this is straightforward:

grep "word" file.txt        

For example, let’s say we have a file named "article.txt" that contains the following text:

The quick brown fox jumps over the lazy dog        

To match the word “fox”, we can use the following command:

grep "fox" article.txt        

This will output:

The quick brown fox jumps over the lazy dog        
No alt text provided for this image

Matching multiple words:

To match multiple words or phrases, you can use the “OR” operator “|”. The syntax for this is:

grep "word1\|word2" file.txt        

Let’s say we want to match both “fox” and “dog” in the previous example. We can use the following command:

grep "fox\|dog" article.txt        

This will output:

The quick brown fox jumps over the lazy dog        
No alt text provided for this image

Matching a pattern:

Regular expressions can also be used to match patterns. For example, let’s say we have a file named "numbers.txt" that contains the following text:

123-456-7890
555-555-1234
888-999-0000        

We can use the following command to match all phone numbers:

grep "[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}" numbers.txt        

This will output:

123-456-7890
555-555-1234
888-999-0000        
No alt text provided for this image

Matching a range of characters:

To match a range of characters, you can use square brackets. For example, let’s say we have a file named "colors.txt" that contains the following text:

red
blue
green
yellow        

We can use the following command to match all colors that start with the letter “g”:

grep "g[a-z]*" colors.txt        

This will output:

green        
No alt text provided for this image

Matching the beginning or end of a line:

You can use the caret (^) to match the beginning of a line and the dollar sign ($) to match the end of a line. For example, let’s say we have a file named "names.txt" that contains the following text:

John Smith
Jane Doe
Michael Johnson        

We can use the following command to match all names that start with “J”:

grep "^J[a-zA-Z]*" names.txt        

This will output:

John Smith        
No alt text provided for this image

Practice text for the 5 regular expressions:

Foxes are quick and dogs are lazy. The quick brown fox jumps over the lazy dog.
The sky is blue and the grass is green. I love the color green.
My phone number is 123-456-7890. Call me anytime.
I like the colors red, blue, and green. Green is my favorite color.
John Smith is a great guy. Jane Doe and Michael Johnson are also nice people.        

In conclusion, understanding the basics of regular expressions and grep can greatly enhance your productivity and efficiency in the Linux terminal. The 5 regular expressions we covered in this article should be a good starting point for anyone who wants to improve their text searching and manipulation skills.

Remember that regular expressions are powerful tools and can be quite complex. It takes time and practice to master them. Therefore, I encourage you to keep practicing and experimenting with different regular expressions and grep commands.

Finally, if you want to learn more about regular expressions, I recommend checking out the documentation for grep and the regex library. There are also plenty of online resources and tutorials that can help you take your regex skills to the next level.

要查看或添加评论,请登录

KEVIN VANEGAS的更多文章

  • LittlePivoting-Dockerlabs Walkthrough / WriteUp

    LittlePivoting-Dockerlabs Walkthrough / WriteUp

    Hello everyone, we continue exploring the Dockerlabs DockerLabs platform that I introduced in my previous video! This…

    5 条评论
  • AS-REP roasting with Crackmapexec/NetExec

    AS-REP roasting with Crackmapexec/NetExec

    This new Crackmapexec/NetExec series is a part of the Active Directory Lab series on my channel . With tools like…

  • Vulnerable Active Directory Lab

    Vulnerable Active Directory Lab

    I'm currently learning pentesting techniques for Active Directory, and I've been sharing my progress on YouTube. Today,…

  • Enumeration With BloodHound

    Enumeration With BloodHound

    As a cybersecurity student, I'm constantly seeking ways to expand my knowledge and skill set. Recently, I delved into…

  • Learning About DCSync Attacks in Active Directory

    Learning About DCSync Attacks in Active Directory

    I continued diving into exploitation techniques within Active Directory. Here are the key highlights: 1?? DCSync…

  • Set Up and Test ASREProast and Kerberoasting Attacks

    Set Up and Test ASREProast and Kerberoasting Attacks

    We create a small Active Directory lab using VirtualBox and a Windows Server Standard evaluation. We'll configure it to…

  • My notes on solving 'Return' from Hack The Box.

    My notes on solving 'Return' from Hack The Box.

    > The machine Key Concepts: Windows Remote Management (WinRM), SMB (Server Message Block), Enumeration, Printer Admin…

  • Creating a basic bash script

    Creating a basic bash script

    I'll show you how to create your first Bash script to perform a small fuzzing operation with the goal of gaining access…

  • Linux `find` Command

    Linux `find` Command

    ███████████████████████████████ ███ Linux `find` Command ███…

  • Cross-site WebSocket hijacking (CSWSH) | RC4 encryption

    Cross-site WebSocket hijacking (CSWSH) | RC4 encryption

    ?? Cybersecurity Journey Update: Inkplot Challenge Completed on HackMyVM! ?? Watch the video here: Link to Video I took…

社区洞察

其他会员也浏览了