Linux Create And Remove Directory
Linux chown Command In Hindi : chown Command Examples
What is Hub? and How Hub works?
Understanding setInterval In JavaScript | JS setInterval In Hindi
Web Performance In Hindi : Website Performance Kya Hai?
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Image on pixabay
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 [options] pattern [files]
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
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 करता है।
अगर आप सिर्फ वो files देखना चाहते हैं , जिसका content pass किये गए pattern से match हुआ है , तो आप -l
option pass कर सकते हैं।
$ grep -l "food" * test.txt
Note* यहां पर * का मतलब है कि current directory में जितनी files होगी सभी में pattern search होगा। हालाँकि आप चाहे तो किसी और directory की location भी pass कर सकते हैं ।
-c
/ --count
option की help से आप सभी matching words की संख्या print करा सकते हैं -
$ grep -c "crow" test.txt 2
सिर्फ matched pattern को print करने के लिए --only-matching
/ -o
का use किया जाता है।
$ grep -o "crow" test.txt crow crow
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.
किसी 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 कर सकते हैं।
Loading ...
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook