{"id":407,"date":"2018-07-06T05:06:11","date_gmt":"2018-07-06T05:06:11","guid":{"rendered":"http:\/\/linuxresellerwebhosting.in\/blog\/?p=407"},"modified":"2018-08-20T05:06:52","modified_gmt":"2018-08-20T05:06:52","slug":"linux-sed-command-with-examples","status":"publish","type":"post","link":"https:\/\/linuxresellerwebhosting.in\/blog\/linux-sed-command-with-examples\/","title":{"rendered":"Linux Sed Command With Examples"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>Today we are going to see about &#8220;SED&#8221; command with examples.<\/p>\n<p>What is SED ?<\/p>\n<p><b>sed<\/b>\u00a0is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file, or input from a pipeline)<\/p>\n<p>We can do more text operations using SED command. But in this article we mainly focus on SED substitution operations.<\/p>\n<p>SYNTAX for Sed Substitution :<\/p>\n<pre class=\"\">#sed 'ADDRESSs\/REGEXP\/REPLACEMENT\/FLAGS' filename\r\n#sed 'PATTERNs\/REGEXP\/REPLACEMENT\/FLAGS' filename<\/pre>\n<ul>\n<li>s is substitute command<\/li>\n<li>\/ is a delimiter<\/li>\n<li>REGEXP is regular expression to match<\/li>\n<li>REPLACEMENT is a value to replace<\/li>\n<\/ul>\n<p>FLAGS can be any of the following<\/p>\n<ul>\n<li>g Replace all the instance of REGEXP with REPLACEMENT<\/li>\n<li>n Could be any number,replace nth instance of the REGEXP with REPLACEMENT.<\/li>\n<li>p If substitution was made, then prints the new pattern space.<\/li>\n<li>i match REGEXP in a case-insensitive manner.<\/li>\n<li>w file If substitution was made, write out the result to the given file.<\/li>\n<li>We can use different delimiters ( one of @ % ; : ) instead of \/<\/li>\n<\/ul>\n<p>Let&#8217;s see some interesting examples.<\/p>\n<ol>\n<li>Substitute Word \u201cLinux\u201d to \u201cLinux-Unix\u201d Using sed<\/li>\n<\/ol>\n<pre class=\"\">echo \"Linux Sysadmin, Linux Scripting etc\" | sed 's\/Linux\/Linux-Unix'\r\n\r\noutput &gt; Linux-Unix Sysadmin, Linux Scripting etc<\/pre>\n<p>In the example below, in the output line \u201cLinux-Unix Sysadmin, Linux Scripting etc\u201d only first Linux is replaced by Linux-Unix. If no flags are specified the first match of line is replaced.<\/p>\n<p>2. Substitute all Appearances of a Word Using sed s<\/p>\n<pre class=\"\">echo \"Linux Sysadmin, Linux Scripting etc\" | sed 's\/Linux\/Linux-Unix\/g'\r\n\r\nOutput &gt; Linux-Unix Sysadmin, Linux-Unix Scripting etc<\/pre>\n<p>The below sed command replaces all occurrences of Linux to Linux-Unix using global substitution flag \u201cg\u201d.<\/p>\n<p>3. Substitute Only 2nd Occurrence of a Word Using sed<\/p>\n<pre class=\"\">echo \"Linux Sysadmin, Linux Scripting etc\" | sed 's\/Linux\/Linux-Unix\/2'\r\n\r\nOutput &gt; Linux Sysadmin, Linux-unix Scripting etc<\/pre>\n<p>In the example below, in the output line \u201c Linux Sysadmin, Linux-Unix Scripting etc.\u201d only 2nd occurance of Linux is replaced by Linux-Unix.<\/p>\n<p>4. Write Changes to a File and Print the Changes Using sed<\/p>\n<pre class=\"\">echo \"Linux Sysadmin, Linux Scripting etc\" | sed 's\/Linux\/Linux-Unix\/gw output'<\/pre>\n<p>The example below has substitution with two flags. It substitutes all the occurance of Linux to Linux-Unix and prints the substituted output as well as written the same to the given the file.<\/p>\n<p>5. Substitute Only When the Line Matches with the Pattern Using sed<\/p>\n<pre class=\"\">echo \"Linux Sysadmin, Linux Scripting etc\" | sed '\/\\etc\/s\/Sys.*\/\/g'<\/pre>\n<p>In this example, if the line matches with the pattern \u201cetc\u201c, then it replaces all the characters from \u201cSys\u201d with the empty.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, Today we are going to see about &#8220;SED&#8221; command with examples. What is SED ? sed\u00a0is a stream<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[1],"tags":[],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=407"}],"version-history":[{"count":8,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions\/423"}],"wp:attachment":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}