Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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

How to Configure HTTP/HTTPS Proxy on Linux and Windows (CLI, GUI, Registry, and APT)

Set up proxy access correctly on Windows and Linux for command-line tools, package managers, and system-level use. This guide covers quick commands, persistent configuration, and common troubleshooting steps.

Read More

Linux pmap Command Guide: Inspect Process Memory Maps with Practical Examples

Learn how to use the Linux pmap command to inspect per-process memory mappings, compare output modes, and troubleshoot high memory usage quickly. This updated guide includes workflow steps, command examples, and interpretation tips.

Read More

iostat Command Guide for Linux: 20 Practical Examples for CPU and Disk I/O Troubleshooting

Learn how to use iostat to monitor CPU and storage performance on Linux systems. This revised guide covers installation, 20 practical commands, metric interpretation, and a troubleshooting workflow.

Read More

xargs in Linux: Practical Commands, Safe Patterns, and Pipeline Workflows

This guide explains how to use xargs effectively in Linux/Unix shells, from basic stdin handling to safe file operations and parallel execution. You’ll learn practical command patterns, portability caveats, and safer alternatives.

Read More

Linux mount and umount Commands: Practical Examples, /etc/fstab, and Safe Unmounting

Learn how to mount and unmount filesystems reliably on Linux, from one-off commands to persistent /etc/fstab entries. This guide covers common filesystem types, command comparisons, and safe troubleshooting steps.

Read More

How to Use gsh to Run Linux Commands Across Multiple Servers

gsh (Group Shell) lets you execute one command across many Linux hosts from a single terminal. This guide explains host grouping with /etc/ghosts, core options, and practical command patterns.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x