MALIKA KAROUM

MALIKA KAROUM
  • Home
  • Inleiding
  • Blog
  • video’s
  • Dubai UAE
  • Promotie
  • Marketing
    • Malika Karoum Strategie Modules
    • Malika Karoum Online Marketing
    • Malika Karoum Business Service
    • Malika Karoum Platform
    • Malika Karoum Marketing Platform
    • Online business marketing
  • Luxury
    • The Indulgence Business site
    • The Luxury Web site
    • The Ultimate Indulgence
    • The Indulgence Site
    • The Ultimate Luxury Information site
    • Online luxury
  • Malika Karoum
    • Malika Karoum LinkedIn
    • Malika Karoum Facebook
    • Malika Karoum Instagram
    • Malika Karoum Business News
    • Malika Karoum Promotion
      • Worth for free now
      • Work from Home 2020
      • Gadgets
      • All about Windows
      • about Whatsapp
      • Whats the
      • About websites
      • New Ways
      • New Way of Watching
      • Virtual
      • Website
      • All about Video
      • How to Use
      • YouTube Info
      • All about Twitter
      • The Best of
      • About Apps
      • Google News
      • For Free
      • About This
      • Need More
      • Why should you
      • Iphone news
      • Interesting News
      • About Amazone
      • Some tips
      • About Netflix
      • All about Music
      • About Facebook
    • Adverteren grote fraude
    • Menu POS
    • Malika Karoum Evenementen
  • Malika Viral
  • Malika Security
  • Home
  • Malika Karoum Global News
  • 10 Practical Examples of the Linux Grep Command
April 17, 2021

10 Practical Examples of the Linux Grep Command

10 Practical Examples of the Linux Grep Command

by Malika Karoum / Saturday, 03 April 2021 / Published in Malika Karoum Global News

The grep command provides access to the grep utility, a powerful file processing tool used to find patterns in text files. It has many practical use cases and is certainly one of the most used Linux commands. This guide illustrates some simple yet useful Linux grep commands that have real-world uses.

Example File for Demonstration

We have created a reference file to help readers understand grep more effectively. You can create a copy of this file by issuing the following shell command in your terminal.

cat <<END >> test-file
This is a simple text file that contains
multiple strings as well as some telephone numbers
(555) 555-1234 (567) 666-2345
and email plus web addresses
john@doe.com
https://google.com
ftp://mywebserver.com
END

1. Find Text in Files

To search for text pattern in a file, simply run grep followed by the pattern name. Also, specify the name of the file that contains the text.

grep "email" test-file

This command will display the line in our test-file that contains the word email. You can also search the same text in multiple files using grep.

grep "example" /usr/share/dict/american-english /usr/share/dict/british-english

The above command displays all instances of the word example in the american-english and british-english dictionary files.

2. Find Exact Match Words

The Linux grep command illustrated in the earlier example also lists lines with partial matches. Use the below-given command if you only need the exact occurrences of a word.

grep -w "string" test-file

The -w or –word-regexp option of grep limits the output to exact matches only. Grep consists of some additional flags that can be used with the default command as well.

Related: How Grep Got Its Name? The History Behind Grep’s Creation

3. Ignore Case Distinctions

By default, grep searches for patterns in a case-sensitive way. However, you may want to turn this off if you don’t know in what case the pattern is beforehand.

grep -i "this" test-file

Use the -i or –ignore-case option for turning off case sensitivity.

4. Count the Number of Patterns

The -c flag stands for count. It displays the number of matches that were found for a particular pattern. Admins can use this for retrieving specific information about the system.

You can pipe the ps command with grep to count the processes that belong to the current user.

ps -ef | grep -c $ USER

The following command displays the number of MP3 files present in a directory.

ls ~/Music | grep -c .mp3

5. Display Line Numbers Containing Matches

You may want to find the line numbers that contain a certain match. Use the -n or –line-number option of grep to achieve this.

cat /etc/passwd | grep -n rubaiat

This option is particularly useful for debugging source codes and troubleshooting log files. To display all the numbers for lines in the ~/.vimrc that are used for configuring the Vim text editor:

grep -n "set" ~/.vimrc

6. Find Filenames Using Extensions

To get a list of all the MP3 files present in the ~/Music directory:

ls ~/Music/ | grep ".mp3"

You can replace .mp3 with any other extensions for locating specific files. The following command lists all the php files present in the current working directory.

ls | grep ".php"

7. Find Patterns in Compressed Files

Linux grep command can also find patterns inside compressed files. You will need to use the zgrep command for doing this, however. First, create a compressed archive of our test-file by typing:

gzip test-file

Now, you can search for text or other patterns inside the resulting archive.

zgrep email test-file.gz

8. Find Email Addresses

Admins can also list email addresses from text files using the Linux grep command. The following example does this by searching for a regular expression pattern.

grep '^[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-z]{2,}' test-file

You can find regular expressions for doing similar jobs or you can create them yourself if you know how they work.

9. Find Phone Numbers Using Grep

You can use grep regular expressions for filtering out phone numbers from a text file. Note that you’d have to tweak the pattern to match the type of phone numbers you need.

grep '(([0-9]{3})|[0-9]{3})[ -]?[0-9]{3}[ -]?[0-9]{4}' test-file

The aforementioned command filters out ten-digit American telephone numbers.

10. Find URLs From Source Files

We can leverage the power of grep for listing out URLs found in text files. The below-given command prints all the URLs present in the test-file.

grep -E "^(http|https|ftp):[/]{2}([a-zA-Z0-9-.]+.[a-zA-Z]{2,4})" test-file

We’re again using the -E option for extended regular expressions. You can also use the egrep command to avoid adding this.

egrep "^(http|https|ftp):[/]{2}([a-zA-Z0-9-.]+.[a-zA-Z]{2,4})" test-file

Mastering the Linux Grep Command

We’ve presented several useful examples of the Linux grep command for tackling real-world problems. Although these examples illustrate the power of grep for text processing, you’ll need to master regular expressions if you want to be super productive with grep.

Sometimes Linux users bump into certain situations where they can’t remember the various options related to a command. Hopefully, the Linux operating system provides you with ways to get command-line help for almost every system utility.

MUO – Feed

  • Tweet
Tagged under: Command, Examples, Grep, Linux, Practical

About Malika Karoum

What you can read next

5 Free Positivity Apps to Manage Mental Health and Boost Your Spirits
What Is GrayKey? A Tool That Breaks iPhone Encryption and Passwords
Nvidia Accidentally Disables Its RTX 3060 Cryptocurrency Mining Lock

Malika Karoum Blog

  • You Can Now Use NVIDIA’s GeForce Experience to Optimize Creative Apps

    The April 2021 update of the NVIDIA Studio Driv...
  • Microsoft Edge Canary Arrives on Android

    Microsoft’s Edge browser is its best offe...
  • Facebook Is Now Powered Entirely by Renewable Energy

    Over the last few years, tech giants have been ...
  • Google Search Launches a New Shortcut for Editing Your Query

    Google’s search results page on desktop n...
  • Google Chrome Gets New Features to Boost Your Productivity

    Google has announced some new productivity feat...
  • Reddit Invites Everyone Into Its Bug Bounty Program

    Fancy yourself a dab hand at breaking into webs...
  • Why You Still Can’t Get Hold of a PS5

    The PS5 has been out for a while now. Despite t...
  • Xgimi’s New Horizon Projectors Bring 4K for Under $2,000

    If you’re looking to add ultra high defin...
  • Microsoft Releases Windows 10 Insider Preview Build 21359

    Microsoft’s Windows 10 Preview builds are...
  • Celebrate Earth Day With These 5 Eco-Friendly Mobile Apps

    April 22nd marks Earth Day across the globe, an...
  • The 7 Best Hidden Features in Microsoft Edge

    Microsoft’s all-new Edge browser has managed to...
  • Artfol Finally Makes Its Long-Awaited Debut on iOS

    There isn’t one standout online art platf...
  • Anker Introduces an Affordable, AI-Enabled Webcam for Home Offices

    Anker is expanding its growing lineup of home o...
  • You Can Now Store PS5 Games on External USB Drives

    Sony has made it easier for you to store more P...
  • Siri Reveals When the Next Apple Launch Event Is Happening

    According to Siri, the Apple Event 2021 appears...
  • The FCC Launches a Speed Test App to Measure Broadband Availability

    A new app from the Federal Communications Commi...
  • Facebook Makes It Easier to Find Out Where to Get Your COVID-19 Vaccine

    More and more Americans are becoming eligible t...
  • LG Confirms List of Phones Set to Get Android 12 and Android 13

    LG recently announced that it’s dropping ...
  • Microsoft’s Latest Attack Ad Rips Into the iPad Pro

    It might be 2021, but to someone in Microsoft&#...
  • Sony May Be Planning to Bring Its "Most Popular Franchises" to Mobile

    Sony Interactive Entertainment is currently loo...

MALIKA KAROUM ONLINE MARKETING PLATFORM

Office:
RME HOLDINGS SARL – DUBAI BRANCH

BUSINESS CENTER

Parcel ID: 345-835

Area: Bur Dubai

Sub Area: Burj Khalifa

UNITED ARAB EMIRATES

 

 

 

Malika Karoum Concept

Malika Karoum Projects

  • GET SOCIAL

© 2014 Malika Karoum -United Arab Emirate Dubai- All Rights Reserved

TOP