Commands
  • About
  • Software Development
    • Glossary
    • System Design
      • CAP Theorem
      • Data Migration
      • EAV Model
      • ETL Process
      • Event Sourcing
      • Outbox Pattern
      • SOLID Principles
    • Standards
      • Character Encoding
      • Twirp
    • Elixir
      • IEx
      • Mix
      • Libraries
      • Code Snippets
    • Node.js
      • TypeScript
  • Commands
    • Bookmarks
    • AWS
      • Athena
      • ECS
    • Docker
      • Manage Images
      • Manage Containers
    • File System
      • Grep
      • Search Files
      • Miscellaneous
    • Git
      • Setup New Project
      • Manage Remotes
      • Release Project
    • GPG
    • Kafka
      • Quick Start
    • Kubernetes
    • OpenShift
      • Manage Roles and Bindings
    • PostgreSQL
    • SSL / TLS
      • Common OpenSSL Commands
      • Create Self-signed Certificate
      • Java Truststore and Keystore
    • SSH
      • Key Management
      • Port Forwarding
    • YUM
      • List Repositories
      • List Packages
    • Miscellaneous
      • Network
Powered by GitBook
On this page
  1. Commands
  2. File System

Grep

grep is a handy utility to search lines by patterns.

There are some variations.

egrep can read the patterns as extended regex.

fgrep is the most performant, but it does not understand regex.

To grep compressed file, there are zgrep, zegrep, and zfgrep accordingly.

Useful Command Options

Turn grep into egrep

grep -E <PATTERN> <FILE>

Print Only The Matching Parts

grep -o <PATTERN> <FILE>

# [EXAMPLE]
# Command: echo 'INFO 2020-01-01 Hello World' | grep -Eo '^[A-Z]+'
# Output:  INFO

Print The N Lines Around Each Match

# Print the matching line, and the N lines AFTER.
grep -A <N> <PATTERN> <FILE>

# Print the matching line, and the N lines BEFORE.
grep -B <N> <PATTERN> <FILE>

# Print the matching line, and the N lines AROUND.
grep -C <N> <PATTERN> <FILE>

Print The Mismatching Lines

grep -v <PATTERN> <FILE>
PreviousFile SystemNextSearch Files

Last updated 3 years ago