← Back to Bash Course | Chapter 11: Process Management | Lesson 2 of 12

grep कमांड

grep एक search-and-highlight tool जैसा है जो text की lines पढ़ता है और सिर्फ वे lines दिखाता है जिनमें वह word हो जिसे आप ढूंढ रहे हैं।

In this page:

  1. grep
Syntax
bash
grep [-i] [-n] [-v] "pattern" file_name

grep

grep input में एक pattern से match करती lines ढूंढती है और matching lines print करती है। Common flags में -i (case-insensitive), -v (invert match), -n (line numbers दिखाना), और -r (एक directory पर recursive) शामिल हैं।

यह shell scripting में सबसे ज़्यादा frequently उपयोग होने वाले text-processing tools में से एक है।

Note: grep -v pattern हर वह line print करता है जो match *नहीं* करती — noise filter out करने के लिए useful।

उदाहरण: grep

bash
#!/bin/bash
printf "apple\nbanana\ncherry\navocado\n" > fruits.txt

echo "Lines containing 'a':"
grep "a" fruits.txt

echo "Lines starting with 'a':"
grep "^a" fruits.txt
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Pattern के चारों ओर quotes भूल जाना, इसलिए shell special characters को interpret कर लेती है।
  2. grep को regex characters जैसे . के साथ उपयोग करना और एक plain string की उम्मीद करना; -F उपयोग करें।
  3. grep results को एक if में बिना -q के check करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.