← Back to Bash Course | Chapter 7: String Manipulation | Lesson 3 of 10

Search और Replace

Parameter expansion आपको एक string के उन हिस्सों को swap out करने देता है जो एक pattern से match करते हैं किसी और चीज़ से, चाहे सिर्फ पहला match हो या string में हर match।
Syntax
bash
${string/old/new}
${string//old/new}
${string/#old/new}
${string/%old/new}

पहली Occurrence Replace करना

${s/old/new} string में old search करता है और सिर्फ पहले match को new से replace करता है, बाद की किसी भी occurrences को untouched छोड़ते हुए। यह search-and-replace parameter expansion का single-slash form है।

उदाहरण: Replacing the First Occurrence

bash
#!/bin/bash
text="cat sat on the cat mat"
result=${text/cat/dog}
echo "$result"

सभी Occurrences Replace करना

${s//old/new} (double slash के साथ) पूरी string में हर occurrence of old को replace करता है, सिर्फ पहले को नहीं। यह go-to form है जब आपको एक global find-and-replace चाहिए।

उदाहरण: Replacing All Occurrences

bash
#!/bin/bash
text="cat sat on the cat mat"
result=${text//cat/dog}
echo "$result"

Start या End पर Anchoring करना

${s/#old/new} सिर्फ old को replace करता है अगर यह string की बिल्कुल शुरुआत से match करे, और ${s/%old/new} सिर्फ इसे replace करता है अगर यह बिल्कुल end पर match करके खत्म हो।

यह बिना गलती से बीच में एक matching substring को touch किए एक prefix/suffix trim या fix करने के लिए उपयोगी है।

उदाहरण: Anchoring to Start or End

bash
#!/bin/bash
filename="test_test_file"
start_replaced=${filename/#test_/demo_}
echo "Start replaced: $start_replaced"
end_replaced=${filename/%file/data}
echo "End replaced: $end_replaced"

कुछ भी ना के साथ Replace करके Matches Delete करना

Replacement text पूरी तरह छोड़ देना, ${s//old/}, effectively string से हर occurrence of old delete करता है क्योंकि यह एक empty string से replace हो रहा है। Unwanted characters या substrings strip करने का यह एक quick तरीका है।

उदाहरण: Deleting Matches by Replacing With Nothing

bash
#!/bin/bash
phone="555-123-4567"
digits_only=${phone//-/}
echo "Digits only: $digits_only"
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. ${s/old/new} उपयोग करना जब आपका मतलब हर occurrence replace करना था; single-slash सिर्फ *पहला* match replace करता है, सभी occurrences के लिए आपको double-slash ${s//old/new} चाहिए।
  2. यह मान लेना कि ${s/old/new} में old एक regular expression है; यह असल में एक glob-style pattern है (जैसे case या [[ == ]] में), पूरी regex नहीं।
  3. यह भूल जाना कि ${s/#old/new} match को string की शुरुआत से anchor करता है और ${s/%old/new} इसे end से anchor करता है -- उन anchors के बिना, match कहीं भी हो सकता है।
चैप्टर सारांश
  • ${s/old/new} सिर्फ old की पहली occurrence को new से replace करता है।
  • ${s//old/new} old की हर occurrence को new से replace करता है।
  • ${s/#old/new} old को सिर्फ तब replace करता है अगर यह string की बिल्कुल शुरुआत में match करे; ${s/%old/new} सिर्फ तब अगर यह बिल्कुल end में match करे।
  • Pattern side glob wildcards (*, ?, [...]) support करता है, पूरी regular expression syntax नहीं।

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.