0% found this document useful (0 votes)
10 views2 pages

Essential grep Command Guide

The document provides an overview of the grep command, which is used for searching text or patterns in files. It includes syntax, commonly used options, pattern matching techniques, recursive examples, useful combinations, and exit codes. Additionally, it offers practice examples to illustrate various grep functionalities.

Uploaded by

ashrufsd035
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Essential grep Command Guide

The document provides an overview of the grep command, which is used for searching text or patterns in files. It includes syntax, commonly used options, pattern matching techniques, recursive examples, useful combinations, and exit codes. Additionally, it offers practice examples to illustrate various grep functionalities.

Uploaded by

ashrufsd035
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

GREP COMMAND NOTES

===================

Purpose:
--------
grep = Global Regular Expression Print
Used to search for text or patterns inside files.

Syntax:
-------
grep [OPTIONS] PATTERN [FILE...]

1. Basic Usage:
---------------
grep "word" [Link]
-> Shows all lines that contain "word" in [Link].

2. Commonly Used Options:


-------------------------
-i Ignore case (case-insensitive)
-v Invert match — show lines NOT matching pattern
-n Show line numbers
-c Count matching lines
-l Print only filenames that match
-L Print filenames WITHOUT the match
-r, -R Recursive search in directories
-w Match whole words only
-x Match whole lines only
-e Use multiple patterns
-f Take patterns from a file
-A N Print N lines After the match
-B N Print N lines Before the match
-C N Print N lines Before and After the match
-H Show filename with each match
-h Hide filename from output
-o Show only the matching part (not full line)
--color=auto Highlight matched text
-q Quiet mode (no output, exit status only)

3. Pattern Matching (Regex):


----------------------------
^word Line starts with 'word'
word$ Line ends with 'word'
. Any single character
* Zero or more of previous char
[] Match any one inside brackets
[^] Exclude characters in brackets
\| OR operator
\bword\b Match exact word boundaries

Use -E for extended regex (same as egrep):


grep -E "error|fail|critical" [Link]

4. Recursive Examples:
----------------------
grep -r "printf" *.c
grep -r "TODO" /home/user/projects/

5. Useful Combinations:
-----------------------
ps -ef | grep ssh
dmesg | grep -i error
grep -rni "config" /etc/
grep -E "WARN|ERROR|FATAL" logfile

6. Exit Codes:
--------------
0 - Match found
1 - No match found
2 - Error (invalid file, syntax, etc.)

Example:
grep -q "root" /etc/passwd && echo "Found" || echo "Not Found"

7. Practice Examples:
---------------------
grep "John" [Link]
grep -i "priya" [Link]
grep -c "50000" [Link]
grep -E "Ravi|Sara|Omar" [Link]
grep -A1 "Priya" [Link]
grep -v "Ahmed" [Link]

You might also like