Strings के साथ grep
grep एक highlighter जैसा है जो text scan करता है उस word के लिए जिसकी आपको care है। एक string के साथ उपयोग होने पर, यह check करने में मदद करता है कि एक छोटा word एक बड़े word के अंदर छुपा है या नहीं।
In this page:
Syntax
echo "$variable" | grep [-q] "pattern"
grep with Strings
grep text को एक pattern से match करने वाली lines के लिए search करता है और आमतौर पर echo या एक variable के साथ combine किया जाता है यह test करने के लिए कि क्या एक string में एक substring है।
grep -q output suppress करता है और सिर्फ exit status set करता है, जो एक if के अंदर handy है। grep -i match को case-insensitive बनाता है।
Note:
यह test करने के लिए कि क्या एक variable में एक substring है,
if echo "$text" | grep -q "pattern"; then उपयोग करें।उदाहरण: grep with Strings
#!/bin/bash
text="The quick brown fox"
if echo "$text" | grep -q "quick"; then
echo "Found 'quick' in the text"
fi
echo "$text" | grep -o "[a-z]*o[a-z]*"
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Pattern के आस-पास quotes भूल जाना, इसलिए shell इसे expand कर देता है।
- एक
ifमेंgrepउपयोग करना-qके बिना, इसलिए matches screen पर print होते हैं। - यह उम्मीद करना कि
grepwhole words match करे, जब यह substrings match करता है जब तक आप-wउपयोग न करें।
🔒
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: