Theory
एक Editor खोले बिना Editing
grep lines ढूँढता है; अक्सर आप फिर उन्हें बदलना चाहते हैं, एक word replace करना, कुछ lines delete करना, reformat करना, एक पूरी file के across, automatically। nano में file खोलकर हाथ से edit करना हज़ारों lines तक या एक script के अंदर चलने तक scale नहीं करता।
sed, वह stream editor, इसका answer है। यह text को edit करता है जैसे यह इसके पास से flow करता है, हर line को आपके instructions के according transform करते हुए, कोई interactive editor शामिल नहीं। इसकी सबसे important skill find-and-replace है, और course का यह closing lesson दिखाता है यह कैसे काम करता है।
Theory
Substitution Command
sed की signature है substitution: s/old/new/। यह old (एक pattern) ढूँढता है और इसे new से replace करता है। Default रूप से यह हर line पर सिर्फ़ पहला match replace करता है।
g flag add कीजिए, s/old/new/g, और यह हर line पर सारे matches replace करता है ('g' global के लिए)। एक पूरी file या stream पर apply किया गया यह एक command, हर जगह एक term rename कर सकता है, पूरे में एक typo fix कर सकता है, या data reformat कर सकता है। Pattern part एक regular expression है, तो regex के बारे में जो आपने सीखा वह यहाँ भी apply होता है।
Practical
sed Substitution और Deletion (verified)
# Replace only the FIRST match on the line
$ echo "cat cat cat" | sed 's/cat/dog/'
dog cat cat
# Replace ALL matches with the g flag
$ echo "cat cat cat" | sed 's/cat/dog/g'
dog dog dog
# Delete line 2 with a line address
$ printf "one\ntwo\nthree\n" | sed '2d'
one
threeFormula
g Flag, और Line Addresses
दो ideas ज़्यादातर sed unlock करते हैं। g flag scope decide करता है: s/old/new/ per line पहला match बदलता है, s/old/new/g हर match बदलता है। 'all' मतलब रखते हुए g भूलना सबसे common sed slip है।
Line addresses decide करते हैं एक command WHERE act करता है: 2d line 2 delete करता है, /ERROR/d ERROR match करने वाली हर line delete करता है, और 1,5s/old/new/g सिर्फ़ lines 1 से 5 में substitute करता है। यह भी नोट कीजिए: default रूप से sed अपना result screen पर print करता है और file नहीं बदलता; file को in place edit करने के लिए -i add कीजिए (carefully, कोई undo नहीं है)।
Quiz
echo "cat cat cat" | sed 's/cat/dog/' क्या output करता है (नोट कीजिए: कोई g flag नहीं)?
- dog dog dog, हर cat replace करते हुए
- dog cat cat, line पर सिर्फ़ पहला cat replace करते हुए
- cat cat cat, unchanged
- एक error, क्योंकि sed को एक file चाहिए
Show the answer
dog cat cat, line पर सिर्फ़ पहला cat replace करते हुए
g flag के बिना, sed का s/old/new/ हर line पर सिर्फ़ पहला match replace करता है, तो 'cat cat cat', 'dog cat cat' बन जाता है, सिर्फ़ पहला cat बदलता है। Option A सिर्फ़ g flag (s/cat/dog/g) के साथ correct होता, जो सारे matches replace करता है 'dog dog dog' देते हुए; वह एक letter difference है। Option C ग़लत है: sed substitution actually perform करता है; line unchanged नहीं छोड़ी जाती। Option D ग़लत है: sed एक pipe (standard input) से खुशी से पढ़ता है, इसे एक file argument नहीं चाहिए। Rule याद रखिए: कोई g का मतलब है सिर्फ़ पहला match; line पर सारे matches के लिए g add कीजिए।
Think first
File को हाथ से Edit करने की बजाय sed क्यों इस्तेमाल करें?
एक छोटे change के लिए, nano आसान है। sed better choice कब और क्यों है? फिर tap कीजिए।
Show the answer
sed तब जीतता है जब edit AUTOMATIC, REPEATABLE, या SCALE पर applied होनी चाहिए, क्योंकि यह non-interactive है और pipelines और scripts में fit होता है। nano में हाथ से edit करना एक file और एक change के लिए fine है, पर यह scale नहीं करता: 500 configuration files में एक term rename करना imagine कीजिए, या हर रात एक log में same typo fix करना, या एक pipeline से गुज़रते हुए data transform करना, कोई human हर file नहीं खोलने वाला और retype नहीं करने वाला। sed इसे एक command में करता है, और क्योंकि यह अपने instructions command line पर लेता है, आप इसे एक SCRIPT में रख सकते हैं और इसे identically हज़ार बार चला सकते हैं, या इसे एक pipeline में grep के बाद chain कर सकते हैं (interesting lines grep कीजिए, फिर इन्हें reshape करने के लिए sed)। यह उस data पर भी काम करता है जो कभी एक file बनता ही नहीं, commands के बीच flow करता text। Trade-off यह है कि hand-editing आपको हर change देखने और judge करने देती है, जबकि sed अपना rule blindly apply करता है, तो आप substitution पहले test करते हैं (बिना -i, screen output देखते हुए) इसे in place commit करने से पहले। Short में: एक one-off tweak के लिए, एक editor fine है; automated, bulk, या एक pipeline या script के अंदर किसी भी चीज़ के लिए, sed सही, powerful tool है। Edit automate कीजिए, और यह किसी भी size तक scale करता है।
Theory
BCA501 Complete: आप Terminal पर जी सकते हैं
आप एक Linux box navigate करने में असमर्थ शुरू हुए और अब आप कर सकते हैं: file system के around move कर सकते हैं, files और permissions manage कर सकते हैं, processes control कर सकते हैं, jobs schedule कर सकते हैं, logic और loops वाली shell scripts लिख सकते हैं, और text search और transform करने के लिए grep और sed से regular expressions wield कर सकते हैं। यही इस course का पूरा arc है। एक powerful next step है tools combine करना, उदाहरण के लिए grep "ERROR" log.txt | sed 's/ERROR/PROBLEM/', lines ढूँढते हुए फिर इन्हें reshape करते हुए। Campus server पर practise करते रहिए; fluency daily use से आती है।
Summary
Key takeaways
- sed एक stream editor है: यह text को इसके पास से flow करते हुए transform करता है, non-interactively, खोलने के लिए कोई editor नहीं।
- इसकी signature substitution है: sed 's/old/new/' हर line पर पहला match replace करता है।
- g flag add करना, sed 's/old/new/g', हर line पर सारे matches replace करता है।
- Line addresses select करते हैं कहाँ act करना है: 2d line 2 delete करता है, /pattern/d matching lines delete करता है, 1,5s/// range से limit करता है।
- Default रूप से sed अपना result print करता है और file नहीं बदलता; -i in place edit करता है (कोई undo नहीं, तो पहले test कीजिए)।
- sed automatic, repeatable, large-scale edits के लिए और pipelines के अंदर (अक्सर grep के बाद) shine करता है।
- Memory hook: s/old/new/ पहला match, सारे के लिए g add कीजिए; -i से पहले test कीजिए।