DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Grep Quotes in Linux

Grep Quotes in Linux

Grep is your friend, and a powerful tool. This article looks at some common scenarios for grep and how it is used to address them.

Madhuka  Udantha user avatar by
Madhuka Udantha
CORE ·
Jul. 18, 16 · DevOps Zone · Code Snippet
Like (20)
Save
Tweet
8.54K Views

Join the DZone community and get the full member experience.

Join For Free

Count line when words have been matched:

$ grep -c 'word' /path/to/file

Pass the -n option to precede each line of output with the number of the line in the text file:

$ grep -n 'root' /etc/passwd

Ignore word case:

$ grep -i 'word' /path/to/file

Use grep recursively under each directory:

$ grep -r 'word' /path/to/file

Use grep to search 2 different words:

$ egrep -w 'word1|word2' /path/to/file

Grep invert match:

$ grep -v 'word' /path/to/file

You can force grep to display output in colors, enter:

$ grep --color 'word' /path/to/file

You can limit the results count:

$ grep -m 10 'word' /path/to/file

You can match regular expression in files (Syntax: grep "REGEX" filename):

$ grep 'word1.*word2' /path/to/file
  • ? The preceding item is optional and matched at most once.
  • * The preceding item will be matched zero or more times.
  • + The preceding item will be matched one or more times.
  • {n} The preceding item is matched exactly n times.
  • {n,} The preceding item is matched n or more times.
  • {,m} The preceding item is matched at most m times.
  • {n,m} The preceding item is matched at least n times, but not more than m times.

Display N lines Around Match

Grep can display N lines after match (Syntax: grep -A <N> "string" filename):

$ grep -A 2 'word' /path/to/file

The following example prints the matched line, along with the two lines after it.

$ grep -A 2 -i 'word' /path/to/file

-C is the option which prints the specified N lines before the match.

$ grep -C 2 'word' /path/to/file
Grep Linux (operating system)

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Test JavaScript Code in a Browser
  • What Is Lean Software Development
  • Flutter vs React Native. How to Cover All Mobile Platforms in 2022 With No Hassle
  • Suspicious Sortings in Unity, ASP.NET Core, and More

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo