• Home
  • About
    • Delta Media Hosting
    • Delta Media Core-business
    • Delta Media
    • Adverteren grote fraude
    • Menu POS
    • Delta Global Security
    • Delta Evenementen
    • LinkedIn
    • Facebook
    • Facebook Group
    • Twitter
    • Terms & Policy
    • Contact
    • .
  • Online Project
  • Delta Online Streaming
    • Sidney Samson Project
    • Sidney Samson Site
    • DJ Online Streaming
    • DJ online Streaming Platform
    • DJ Online Streaming Site
    • Online Music Streaming
    • The Online Streaming Platform
    • Online Streaming Radio
    • Streaming Service
    • Digitale Radio
    • DJ Video Platform
    • Online Streaming Service
    • Online Streaming Info
    • Online Streaming Platform
    • More Ledo links
  • Marketing Platform
    • Delta Media Strategie Modules
    • Online Marketing
    • Delta Social Media
    • Delta Media Service
    • Delta media Platform
    • Delta Marketing Platform
    • Online business marketing
    • Online Business
      • 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
  • Luxury Platform
    • The Indulgence Business site
    • The Luxury Web site
    • The Ultimate Indulgence
    • The Indulgence Site
    • The Ultimate Luxury Information site
    • Online luxury
  • Projecten
    • Global Diamond Security
    • Aqualith Project
    • Delta Online Projects
    • Delta Media
    • Delta Media Projects
    • Crypto info
      • What is Cryptocurrency
      • Delta Media ICO systeem
      • Cryptocurrency Information
      • About miners
      • Best Bitcoin Bokers
      • Overview Cryptocurrency
      • BLOCKCHAIN WEB PLATFORM
      • Delta Media ICO Cryptocurrency
      • Bitcoin Ticker
      • ICO and Cryptocurrency modules
      • Cryptocurrency, Blockchain, Bitcoin modules
      • Delta Buy & Sale Token
      • Buy and sell digital currency module
      • ico – Crypto BlockChain Parallax module
      • Exchange Cryptocurrency module
  • Nieuws
    • RegioTV Nieuws
    • Regionaal Nieuws Platform
    • Nieuws Regio’s Tiel
    • RegioTV Buren
    • RegioTV Neder-Betuwe
    • Regionaal Nieuws Tiel
    • Micro Locals Nieuws
    • Micro Locals project
    • Lokale CSM systeem
    • RegioTV nieuws
    • Politiek Nederland
    • Politiek Gelderland
    • Politiek Tiel
    • Regio Nieuwsberichten Tiel
    • Regionaal nieuws
    • Regionaal video nieuws
    • RegioTV Nieuws & info
    • Regio Nieuws
    • Online video nieuws
    • Online Nieuws
    • Nieuws online RegioTV
    • Regionale content video
  • Promotie
  • Ads prices
    • Online Blog promotion price
    • Online web sites prices
    • Global Promotion Platform
  • Home
  • Delta Media News
  • 10 Practical Examples of the Linux Grep Command
April 18, 2021

10 Practical Examples of the Linux Grep Command

10 Practical Examples of the Linux Grep Command

by rudy deighton / Saturday, 03 April 2021 / Published in Delta Media 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 rudy deighton

What you can read next

Facebook Dating Now Has a “Secret Crush” Feature
How to Set Google as Your Homepage in Any Web Browser
8 Bots You Should Add to Your Facebook Messenger App

Rudy Deighton Corporate Blog

  • Apple Highlights Environmental Benefits of Not Selling iPhones With Power Adapters

    Following the launch of the iPhone 12 last year...
  • The 6 Best Laptop Fan Control Apps to Keep Your Laptop Cool

    Don’t have a dedicated fan control softwa...
  • How to Configure Display Scaling on Windows 10 for High-DPI Monitors

    It is not a secret that Windows doesn’t look go...
  • How to Use CSS box-shadow: 13 Tricks and Examples

    CSS is the language developers use to style a w...
  • 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...
  • How to Include Emojis in Your Python Code

    An emoji is a small digital image used to expre...
  • The Samsung Galaxy S21 FE Has Leaked in Renders

    Renders of Samsung’s upcoming “Fan ...
  • Study: The US Is Spreading COVID-19 Misinformation to Canada

    Canadian prime minister Justin Trudeau once des...
  • The Official 2021 White House Portraits Were Shot With the Sony a9 II

    Weeks after the inauguration of a new US presid...
  • The 5 Best Music Visualizers for Android

    Music visualizers can help you bring your music...
  • Logitech Discontinues Its Harmony Line of Universal Remotes

    Logitech has axed its Harmony remote lineup for...
  • Google Announces the Pixel 5a by Denying It Has Cancelled the Pixel 5a

    Following reports that the Pixel 5a launch has ...
  • Report: Hackers Are Sending Fake Job Offers on LinkedIn to Try and Steal Your Data

    The unfortunate rise in unemployment caused by ...
  • 3 Great Tools to Keep You Focused While Working From Home

    Whether you’re working from home, writing...
  • The 7 Best Wireless Lavalier Microphones

    Summary List 9.60/10 1. Premium pick: Shure GLX...
  • Facebook Starts Applying Labels to Pages to Avoid Confusion

    Facebook is slowly adding new labels to certain...
  • How to Manage Tab Groups in Google Chrome

    Tab Groups from Google Chrome are changing the ...
  • 5 Exciting Linux Distro Updates to Look Forward to in 2021

    We’re four months into 2021 and a lot of ...
  • Google I/O Returns in May, Virtually, and Free for the First Time

    Google has announced that it will be holding I/...
  • Clubhouse Now Lets You Send Payments to Creators

    Online voice chat has become a more widespread ...
  • How to Set Up Strava and Record Your Walks

    Monitoring your progress can be a great motivat...

DELTA MEDIA ONLINE MARKETING PLATFORM
An Arte di Riunire Investments GmbH Division

Office:
Delta Media

Arte di Riunire Investments GmbH

Address: Lood 207F

Postcode: 3803 Beatenberg (Swiss)

email: rudydeighton@hotmail.com

rudydeighton@deltamediagbe.com

 

 

Online Platform

Menu POS Systeem

  • GET SOCIAL

© 2014 Delta Media - Arte di Riunire Investments GmbH Division - SWISS All Rights Reserved

TOP