Pattern Matching using grep, egrep, and fgrep

grep वह command है जो text को एक pattern के लिए search करता है और matching lines print करता है: plain grep basic regex इस्तेमाल करता है, egrep (grep -E) extended regex इस्तेमाल करता है, और fgrep (grep -F) एक fixed literal string को तेज़ी से match करता है, और साथ मिलकर यही तरीका है आप logs search करते हैं।

11 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Logs Search करना

Campus server की log file में हज़ारों lines हैं, और आपको वे चाहिए जो ERROR कहती हैं। हाथ से scroll करना hopeless है। यह grep का everyday job है: इसे एक pattern और एक file दीजिए, और यह हर वह line print करता है जो match करती है।

grep पूरे Linux के सबसे-इस्तेमाल होने वाले commands में से एक है, और यह तीन flavours में आता है, grep, egrep, और fgrep, जो इस बात में अलग हैं ये आपके pattern को कैसे interpret करते हैं। यह lesson तीनों दिखाता है और उन कुछ flags को जो grep को genuinely powerful बनाते हैं।

At a glance

Commandयह Pattern कैसे पढ़ता है
grepBasic regular expressions (default)
egrep (grep -E)Extended regular expressions: |, +, ?, ( ) escaping के बिना
fgrep (grep -F)एक fixed literal string: कोई regex नहीं, special characters खुद को match करते हैं
-iFlag: case-insensitive matching
-c / -nFlag: matching lines count करो / line numbers दिखाओ
-vFlag: invert, वे lines दिखाओ जो match NAHI करतीं

Practical

एक Log Search करता grep (verified)

$ grep "ERROR" log.txt          # lines containing ERROR
ERROR disk full
ERROR timeout
$ grep -c "ERROR" log.txt       # count them instead
2
$ grep -i "info" log.txt        # case-insensitive: INFO and info
INFO started
info lowercase
$ grep -E "ERROR|WARN" log.txt  # extended: ERROR or WARN
ERROR disk full
ERROR timeout
WARN low memory

Formula

अपने Pattern से Match करने वाला Variant पिक कीजिए

इससे choose कीजिए आपके pattern को क्या चाहिए। एक simple pattern या basic regex के लिए plain grep इस्तेमाल कीजिए। egrep (या grep -E) इस्तेमाल कीजिए जब आपका pattern | (or), +, या grouping जैसे extended features इस्तेमाल करता है, ताकि आप इन्हें बिना backslashes के लिख सकें। fgrep (या grep -F) इस्तेमाल कीजिए जब आप एक literal string exactly match करना चाहते हैं, . और * जैसे characters को खुद की तरह treat करते हुए, regex की तरह नहीं।

और flags याद रखिए: -i case ignore करता है, -c count करता है, -n lines number करता है, -v दिखाता है क्या match NAHI होता। ये grep को एक finder से एक precise investigative tool में बदलते हैं।

Quiz

आप count करना चाहते हैं log.txt में कितनी lines में ERROR word है, case-sensitively। कौन सी command best है?

  1. grep -v "ERROR" log.txt, जो matches count करता है
  2. grep -c "ERROR" log.txt, जो matching lines की number print करता है
  3. grep -i "ERROR" log.txt, जो matches count करता है
  4. fgrep बिना किसी flag के automatically count करता है
Show the answer

grep -c "ERROR" log.txt, जो matching lines की number print करता है

grep -c matching lines का COUNT print करता है, तो grep -c "ERROR" log.txt directly number देता है (यहाँ, 2)। Option A -v इस्तेमाल करता है, जो match को INVERT करता है, यह ERROR के बिना वाली lines दिखाएगा या count करेगा, आप जो चाहते हैं उसका opposite। Option C -i (case-insensitive) इस्तेमाल करता है और, ज़्यादा importantly, count नहीं करता, यह matching lines खुद print करता है; -i lowercase में 'error' भी match करेगा, 'case-sensitive' requirement break करते हुए। Option D ग़लत है: कोई भी grep variant बिना -c flag के count नहीं करता। Flag को task से match कीजिए: count के लिए -c, case-insensitive के लिए -i, invert के लिए -v, line numbers के लिए -n।

Think first

अगर grep पहले से search कर सकता है, fgrep क्यों Exist करता है?

grep patterns handle करता है, तो fixed strings के लिए fgrep क्यों? 'Fixed' आपको क्या देता है? फिर tap कीजिए।

Show the answer

fgrep (grep -F) आपके pattern को एक LITERAL string की तरह treat करता है, जो special characters के लिए दोनों SAFER है और FASTER है। Safety point: . * [ ] ^ $ जैसे regex characters normal grep में special meaning रखते हैं, तो एक real dotted string जैसे एक IP address '192.168.1.1' को plain grep से search करना हर . को ANY character match करने देगा, false matches देते हुए। fgrep के साथ, ये characters literally लिए जाते हैं, तो 'a.b' सिर्फ़ एक real 'a.b' match करता है, 'aXb' नहीं। यही exactly वजह है आप fgrep की तरफ़ reach करते हैं जब आपके search text में regex metacharacters हों जिन्हें आप ordinary text की तरह treated चाहते हैं (file paths, IP addresses, code snippets)। Speed point: क्योंकि fgrep को एक regular expression interpret नहीं करनी पड़ती, बस एक fixed string ढूँढनी है, यह faster हो सकता है, जो बहुत बड़ी files scan करते हुए matter करता है। तो fgrep सही tool है जब आप जानते हैं आपका target एक exact literal string है: आप accidental regex surprises avoid करते हैं और एक quick, predictable search पाते हैं। अगर आपके text में dots, brackets, या asterisks हैं जो आप literally मतलब रखते हैं, fgrep आपको हर एक को escape करने से बचाता है। Literal और fast: यही fgrep का niche है।

Summary

Key takeaways

  • grep text को एक pattern के लिए search करता है और matching lines print करता है, logs और files search करने का everyday तरीका।
  • grep basic regex इस्तेमाल करता है; egrep (grep -E) extended regex इस्तेमाल करता है (|, +, ?, escaping के बिना grouping)।
  • fgrep (grep -F) एक fixed literal string match करता है: special characters literally लिए जाते हैं, और यह fast है।
  • Key flags: -i case-insensitive, -c matching lines count, -n line numbers दिखाओ, -v invert (non-matching), -r recursive।
  • grep -c "ERROR" matching lines count करता है; grep -i case के बावजूद match करता है; grep -v दिखाता है क्या match नहीं होता।
  • Regex characters (paths, IPs) वाले literal text के लिए fgrep इस्तेमाल कीजिए accidental matches avoid करने के लिए।
  • Memory hook: grep basic, egrep extended, fgrep fixed; -i -c -n -v everyday flags हैं।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Advanced Text Processing Tools

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Pattern Matching using grep, egrep, and fgrep · Linux Operating System (LOS) (Minor-04) · Gri-Learn