{"id":2848,"date":"2025-02-11T16:11:02","date_gmt":"2025-02-11T16:11:02","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2848"},"modified":"2025-02-12T00:03:10","modified_gmt":"2025-02-12T00:03:10","slug":"sed-stream-editor","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2025\/02\/11\/sed-stream-editor\/","title":{"rendered":"sed &#8211; Stream Editor"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-style-rounded\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1-1024x1024.jpeg\" alt=\"\" class=\"wp-image-2849\" style=\"aspect-ratio:1;object-fit:cover\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1-1024x1024.jpeg 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1-150x150@2x.jpeg 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1-150x150.jpeg 150w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1-1536x1536.jpeg 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1.jpeg 2048w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2025\/02\/sed-1-300x300@2x.jpeg 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the realm of Unix-like operating systems, where command-line prowess reigns supreme, <code>sed<\/code> stands tall as a versatile and indispensable tool. Born in the early 1970s at the legendary Bell Labs, <code>sed<\/code> (short for &#8220;stream editor&#8221;) emerged from the creative mind of Lee E. McMahon. Its creation was inspired by the need for a command-line utility capable of performing substitutions, akin to the functionality found in the <code>grep<\/code> command but with the added ability to modify the matched text.<\/p>\n\n\n\n<p><code>Sed<\/code> operates as a non-interactive stream editor, meaning it processes input line by line without requiring user intervention. This makes it ideal for automating text transformations within scripts or pipelines. Its core strength lies in its ability to apply a series of editing commands to an input stream, be it from a file or standard input, and produce a modified output stream.<\/p>\n\n\n\n<p>Over the decades, <code>sed<\/code> has evolved into a powerful tool for a wide range of text manipulation tasks. From simple find-and-replace operations to complex pattern matching and substitutions, <code>sed<\/code> offers a concise and efficient way to handle text processing needs. Its enduring popularity stems from its simplicity, flexibility, and the ability to seamlessly integrate with other command-line utilities.<\/p>\n\n\n\n<p>Whether you&#8217;re a system administrator automating configuration changes, a developer processing log files, or a data analyst cleaning and transforming data, <code>sed<\/code> is an invaluable tool to have in your arsenal. Its ability to perform complex text manipulations with minimal code makes it a true powerhouse of the command line<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Examples:<\/h3>\n\n\n\n<p>Normally the invocation of sed is as below<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-large-font-size wp-elements-ebe0b403d570ec1fe1a0a53ad78369f1\"><code>sed SCRIPT INPUTFILE<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">I. Reading from stdin and replacing<\/h4>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background has-link-color has-small-font-size wp-elements-df5973a2c24d00db23bc6d8d9e2ee140\"><code>sed 's\/hello\/world\/'  -\nsaurabh hello\nsaurabh world\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">II. Replacing<\/h4>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background has-link-color has-small-font-size wp-elements-65c64bb28d72b6e31100481e2fc25a73\"><code>> echo \"Hello hello\" | sed 's\/hello\/world\/'\nHello world\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">iii. Replacing in file<\/h4>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background has-link-color has-small-font-size wp-elements-47eeb3d5b8c0bf96d210e41a51dde8f9\"><code>$ echo \"hello saurabh\" > test.txt \n$ cat test.txt \nhello saurabh\n$ sed -i \"s\/hello\/world\/\" test.txt \n$ cat test.txt \nworld saurabh\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">iv. Script within script<\/h4>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background has-link-color has-small-font-size wp-elements-7dda9a3a70a396654092b1cd904b79f0\"><code>$ sed -i \"s\/hello\/world\/; s\/world\/World\/\" test.txt \n$ cat test.txt \nWorld saurabh\n$ sed -i \"s\/saurabh\/world\/; s\/World\/word\/\" test.txt \n$ cat test.txt \nword world\n$ <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Help<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.gnu.org\/software\/sed\/manual\/sed.html#Introduction\">https:\/\/www.gnu.org\/software\/sed\/manual\/sed.html#Introduction<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of Unix-like operating systems, where command-line prowess reigns supreme, sed stands tall as a versatile and indispensable tool. Born in the early 1970s at the legendary Bell Labs, sed (short for &#8220;stream editor&#8221;) emerged from the creative mind of Lee E. McMahon. Its creation was inspired by the need for a command-line [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2850,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[112,337],"class_list":["post-2848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-linux","tag-sed"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2848"}],"version-history":[{"count":2,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2848\/revisions"}],"predecessor-version":[{"id":2852,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2848\/revisions\/2852"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2850"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}