← Back to Bash Course | Chapter 10: Text Processing Tools | Lesson 3 of 12

sed की Basics

sed text को line by line पढ़ता है और simple edits apply करता है, सबसे commonly text के pieces find और replace करते हुए।
Syntax
bash
sed 's/old/new/' file_name
sed 's/old/new/g' file_name
sed -i 's/old/new/g' file_name

Basic Substitution

s/old/new/ command हर line पर old के पहले match को new से substitute करता है। एक trailing g flag के बिना, per line सिर्फ पहला occurrence replace होता है।

उदाहरण: Basic Substitution

bash
#!/bin/bash
echo "cat sat on the cat mat" | sed 's/cat/dog/'

Global Substitution

तीसरे slash के बाद एक g flag add करना substitution को line पर मिले सिर्फ पहले match के बजाय हर match पर apply करता है।

उदाहरण: Global Substitution

bash
#!/bin/bash
echo "cat sat on the cat mat" | sed 's/cat/dog/g'

एक File को In Place Edit करना

sed -i 's/old/new/' file stdout पर change print करने के बजाय file को खुद rewrite करता है। यह destructive है, इसलिए पहले एक sample पर plain (non -i) version test करना worth है।

Warning: sed -i बिना confirmation file को overwrite करता है; important files पर इसे चलाने से पहले एक backup रखें या version control उपयोग करें।

उदाहरण: Editing a File in Place

bash
#!/bin/bash
echo "version=1.0" > config.txt
sed -i 's/1.0/2.0/' config.txt
cat config.txt
rm -f config.txt

Lines Delete करना

sed Nd line number N delete करता है। sed '/pattern/d' line में कहीं भी pattern से match करने वाली हर line delete करता है, जो comments या blank lines strip करने का एक quick तरीका है।

उदाहरण: Deleting Lines

bash
#!/bin/bash
printf "keep\n# comment\nkeep too\n# another comment\n" > notes.txt
sed '/^#/d' notes.txt
rm -f notes.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. यह भूल जाना कि sed default रूप से stdout पर print करता है और file को modify नहीं करता जब तक -i उपयोग न हो।
  2. macOS/BSD sed पर sed -i को बिना argument के उपयोग करना और एक error से टकराना, क्योंकि GNU sed और BSD sed के अलग -i syntax हैं; GNU/Linux पर (इस course के sandbox जैसे) -i बिना एक backup suffix दिए अलग से कोई separate backup suffix नहीं लेता।
  3. Pattern में . या / जैसे special regex characters को escape न करना, जिससे sed intended से ज़्यादा (या कम) match करता है।
चैप्टर सारांश
  • sed 's/old/new/' file हर line पर old के पहले occurrence को new से replace करता है।
  • एक g flag add करना (s/old/new/g) हर line पर हर occurrence replace करता है, सिर्फ पहला नहीं।
  • sed -i result को stdout पर print करने के बजाय एक file को in place edit करता है।
  • sed 2d file line 2 delete करता है; sed '/pattern/d' file pattern से match करने वाली हर line delete करता है।
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

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.