grep Command in Linux : What is grep Command ?

Blogs ❯❯ Linux

Image could not load

Image on pixabay

What is grep Command ?

grep full form global search for regular expression है। Linux में grep command का use , pass किये particular pattern के characters के according किसी file or file content को search करके सभी matching lines / lines को display करता है। file content को search करने में जो pattern दिया जाता है है उसे regular expression कहते हैं।

grep Command Syntax
$ grep [options] pattern [files]

grep Command Options

search result को और efficiently बनाने के लिए grep command में और भी कई सारे options available है , जिन्हे आप grep --help command run करके देख सकते हैं।

$ grep --help
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.

-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
.
.
. and some other info about grep

Linux grep Command Example

Example के लिए हम एक test.txt name की file बनाकर उसमे content insert करेंगे , फिर grep command का use करके अलग अलग तरीके से content को filter करने की कोशिश करेंगे।

$ touch test.txt
$ cat > test.txt
There once lived a crow. One day he was very hungry.
He had not been able to get any food the previous day. “If I do not get anything to eat I will starve to death," he thought.
As the crow was searching for food, his eyes fell on a piece of bread. He quickly swooped down, picked it up and flew off.
Far away in a lonely place he sat on a tree to enjoy the bread.
press ctrl+d to save and exit.

Note : Example में use की गयी touch Command का use file create करने के लिए और cat Command का use file में content write और concatenate करने के लिए किया जाता है।

file में content save हो गया है , अब पहले "food" word से सभी lines को search करेंगे।

$ grep -i "food" test.txt
He had not been able to get any food the previous day. “If I do not get anything to eat I will starve to death," he thought.
As the crow was searching for food, his eyes fell on a piece of bread. He quickly swooped down, picked it up and flew off.

-i option content को case insensitively search करता है , मतलब content को capital / small letter दोनों तरह से search करता है।

Display matching file name

अगर आप सिर्फ वो files देखना चाहते हैं , जिसका content pass किये गए pattern से match हुआ है , तो आप -l option pass कर सकते हैं।

$ grep -l "food" *
test.txt

Note* यहां पर * का मतलब है कि current directory में जितनी files होगी सभी में pattern search होगा। हालाँकि आप चाहे तो किसी और directory की location भी pass कर सकते हैं ।

Count matching words

-c / --count option की help से आप सभी matching words की संख्या print करा सकते हैं -

$ grep -c "crow" test.txt
2
Print matched pattern only

सिर्फ matched pattern को print करने के लिए --only-matching / -o का use किया जाता है।

$ grep -o "crow" test.txt
crow
crow
Show line number

matched pattern की lines को line number के साथ print करने के लिए -n / --line-number option का use किया जाता है।

$ grep -n "crow" test.txt
1:There once lived a crow. One day he was very hungry.
3:As the crow was searching for food, his eyes fell on a piece of bread. He quickly swooped down, picked it up and flew off.
Print lines that end with a string

किसी particular string के साथ match होने वाली lines को print करने के लिए string को $ sign के साथ (eg. "string$") pass किया जाया है।

$ grep "hungry.$" test.txt
There once lived a crow. One day he was very hungry.

I Hope, अब आप Linux में grep Command के बारे में अच्छे से समझ गए होंगे , बाकी इसके और भी कई options हैं जिनके साथ आप practice कर सकते हैं।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook