MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Unix grep Quick Reference: Patterns, Options, and Real Command Examples

grep is the standard Unix text-search command for finding lines that match a pattern in one or more files. This guide helps DevOps engineers, sysadmins, and shell users quickly choose the right grep options and write safer, more predictable search commands.

Originally published on 2010-04-16; fully revised and updated.

unix-command-grep

grep syntax at a glance

The base form is pattern-first, then one or more files. If no file is provided, grep reads from standard input.

grep [options] 'pattern' file1 [file2 ...]

# stdin example
journalctl -u sshd | grep -i 'failed'Code language: PHP (php)

Most useful grep options (quick comparison)

OptionWhat it doesExample
-iIgnore case during matchinggrep -i ‘agarwal’ emp.lst
-vInvert match (show lines that do not match)grep -v ‘director’ emp.lst
-nShow line numbers with matched linesgrep -n ‘marketing’ emp.lst
-cCount matching lines (not total match occurrences)grep -c ‘director’ emp.lst
-lList only filenames that contain at least one matchgrep -l ‘manager’ *.lst
-eAdd multiple patterns on the command linegrep -e ‘Agarwal’ -e ‘aggarwal’ emp.lst
-fRead patterns from a file (one pattern per line)grep -f pattern.lst emp.lst

How grep evaluates a search request

flowchart TD
    A[Choose input files or stdin] --> B[Choose pattern source]
    B --> C[Inline pattern]
    B --> D[Pattern file with -f]
    C --> E[Apply options like -i -v -n]
    D --> E
    E --> F{Any matching lines}
    F -- yes --> G[Print selected output]
    F -- no --> H[Return nonzero status]

This flow is useful in scripts: grep output gives you the matching lines, and grep exit status can drive conditional logic.

Practical command examples

# Basic single-file search
grep 'sales' emp.lst

# Search across multiple files
grep 'director' emp1.lst emp2.lst

# Phrase search (quoted pattern)
grep 'jai sharma' emp.lst

# Variable expansion inside double quotes
grep "jai Sharma $var" emp.lst

# Ignore case
grep -i 'agarwal' emp.lst

# Exclude matching lines and write remaining lines to a new file
grep -v 'director' emp.lst > other.list

# Show line numbers
grep -n 'marketing' emp.lst

# Count matching lines
grep -c 'director' emp.lst

# Show only filenames with matches
grep -l 'manager' *.lstCode language: PHP (php)
# Multiple patterns from CLI
grep -e 'Agarwal' -e 'aggarwal' -e 'agarwal' emp.lst

# Patterns from a file (one per line)
grep -f pattern.lst emp.lstCode language: PHP (php)

Common pitfalls and best practices

  • Quote patterns that contain spaces or shell-special characters.
  • Use -i when case is uncertain instead of manually duplicating case variants.
  • Remember: -c counts matching lines, not every repeated occurrence on a line.
  • Use -f for long pattern lists; it is cleaner and easier to review than many -e flags.
  • When redirecting output, use a clear filename (for example, other.list) to avoid shell parsing issues with unquoted spaces.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Unix Epoch Timestamp Guide for DevOps: Conversions, Commands, and Pitfalls

Unix epoch timestamps are simple in concept but easy to misuse in production. This updated guide shows reliable conversions across shell, Python, JavaScript, and SQL, with timezone and Y2038 guardrails.

Read More

Common UNIX Commands and Utilities: Practical Reference for DevOps and Sysadmin Work

A structured UNIX command reference organized by real operational tasks: discovery, files, processes, networking, text processing, and job control. Includes C-shell notes, comparison tables, and practical command examples.

Read More

UNIX sed Quick Reference: Print and Delete Lines with Address and Pattern Ranges

Use this sed quick reference to print or delete exact lines, ranges, and regex matches without opening an editor. It also highlights GNU vs POSIX differences and safer in-place editing patterns.

Read More

Shell Scripting Interview Questions, Explained: Bash Fundamentals, UNIX Processes, and Practical Commands

A modernized, interview-focused guide to shell scripting and core UNIX concepts for DevOps engineers. Learn command execution flow, redirection, permissions, process control, IPC, and safe scripting patterns with practical examples.

Read More

Unix Basics for DevOps Engineers: Commands, Permissions, Pipes, and Workflow

This revised guide turns a legacy Unix slide embed into a practical reference for beginners and DevOps engineers. Learn core Unix concepts, essential commands, and command execution flow with hands-on examples.

Read More

How to Install CollabNet SVN Command Line Client on Solaris

CollabNet provides a Subversion (SVN) command-line client for version control operations. If you’re using Solaris, follow these steps to install the CollabNet SVN client. 1. Prerequisites Before…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
27 days ago

The grep command is a practical tool for finding patterns in files and logs. Options like -i, -n, and -r help make searches more flexible and easier to read. Combining grep with commands such as tail can speed up troubleshooting when reviewing large log files. It is also useful to quote search patterns that contain spaces or special characters. Practicing these options helps make everyday Unix tasks faster and more accurate.

1
0
Would love your thoughts, please comment.x
()
x