{"id":715,"date":"2012-03-23T03:11:30","date_gmt":"2012-03-23T08:11:30","guid":{"rendered":"https:\/\/www.bodhost.com\/blog\/?p=715"},"modified":"2026-02-26T13:59:05","modified_gmt":"2026-02-26T13:59:05","slug":"uses-of-sed-substitution-in-unix-server","status":"publish","type":"post","link":"https:\/\/www.bodhost.com\/blog\/uses-of-sed-substitution-in-unix-server\/","title":{"rendered":"Uses Of Sed Substitution In Unix Server"},"content":{"rendered":"\r\n<p>In this article, let us review some interesting workarounds with the \u201cs\u201d substitute command in sed with several practical examples.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>I. Sed Substitution Delimiter<\/strong><\/h3>\r\n\r\n\r\n\r\n<p>In the sed substitute command, users can use different delimiters like, @ % | ; :<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"300\" class=\"wp-image-724\" title=\"unix-linux\" src=\"https:\/\/www.bodhost.com\/blog\/wp-content\/uploads\/2012\/03\/unix-linux1.jpg\" alt=\"\" srcset=\"https:\/\/www.bodhost.com\/blog\/wp-content\/uploads\/2012\/03\/unix-linux1.jpg 400w, https:\/\/www.bodhost.com\/blog\/wp-content\/uploads\/2012\/03\/unix-linux1-300x225.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>We first need to create the path.txt file which is used in all the given examples<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ cat path.txt\r\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin:\/usr\/bin:\/usr\/sas\/bin\r\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\r\n\/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin\r\n\r\n<\/pre>\r\n\r\n\r\n\r\n<p><strong>Example 1 \u2013 sed @ delimiter: Substitute \/opt\/omni\/lbin to \/opt\/tools\/bin<\/strong><\/p>\r\n\r\n\r\n\r\n<p>If you substitute a path as \u2018\/\u2019 and you can use @ as a delimiter instead of \u2018\/\u2019. In the sed example below, in the last line of the input file, \/opt\/omni\/lbin was changed to \/opt\/tools\/bin.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ sed 's@\/opt\/omni\/lbin@\/opt\/tools\/bin@g' path.txt\r\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin\r\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\r\n<strong>\/opt\/tools\/bin<\/strong>:\/opt\/omni\/sbin:\/root\/bin<\/pre>\r\n\r\n\r\n\r\n<p><strong>Example 2 \u2013 sed \/ delimiter: Substitute \/opt\/omni\/lbin to \/opt\/tools\/bin<\/strong><\/p>\r\n\r\n\r\n\r\n<p>When you should use \u2018\/\u2019 in path name-related substitution, you have to escape \u2018\/\u2019 in the substitution data as shown below. In this sed example, the delimiter \u2018\/\u2019 was escaped in the REGEXP and REPLACEMENT part.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ sed 's\/\\\/opt\\\/omni\\\/lbin\/\\\/opt\\\/tools\\\/bin\/g' path.txt\r\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin\r\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\r\n<strong>\/opt\/tools\/bin<\/strong>:\/opt\/omni\/sbin:\/root\/bin\r\n<\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>II. Sed \u2018&amp;\u2019 Get Matched String<\/strong><\/h3>\r\n\r\n\r\n\r\n<p>The precise part of an input line on which the Regular Expression matches is represented by &amp;, which can then be used in the replacement part.<\/p>\r\n\r\n\r\n\r\n<p><strong>Example 1 \u2013 sed &amp; Usage: Substitute \/usr\/bin\/ to \/usr\/bin\/local<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ sed 's@\/usr\/bin@&amp;\/local@g' path.txt\r\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:<strong>\/usr\/bin\/local<\/strong>:\/usr\/sas\/bin\r\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:<strong>\/usr\/bin\/local<\/strong>:\/opt\/omni\/bin:\r\n\/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin<\/pre>\r\n\r\n\r\n\r\n<p>In the above example \u2018&amp;\u2019 in the replacement part will replace with \/usr\/bin which is matched pattern and add it with \/local. So in the output all the occurrence of \/usr\/bin will be replaced with \/usr\/bin\/local<\/p>\r\n\r\n\r\n\r\n<p><strong>Example 2 \u2013 sed &amp; Usage: Match the whole line<\/strong><\/p>\r\n\r\n\r\n\r\n<p>&amp; replaces whatever matches with the given REGEXP.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ sed 's@^.*$@&lt;&lt;&lt;&amp;&gt;&gt;&gt;@g' path.txt\r\n&lt;&lt;&lt;\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin&gt;&gt;&gt;\r\n&lt;&lt;&lt;\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:&gt;&gt;&gt;\r\n&lt;&lt;&lt;\/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin&gt;&gt;&gt;<\/pre>\r\n\r\n\r\n\r\n<p>In the above example regexp has \u201c^.*$\u201d which matches the whole line. Replacement part &lt;&lt;&lt;&amp;&gt;&gt;&gt; writes the whole line with &lt;&lt;&lt; and &gt;&gt;&gt; in the beginning and end of the line respectively.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>III. Grouping and Back-references in Sed<\/strong><\/h3>\r\n\r\n\r\n\r\n<p>Grouping can be used in sed like normal regular expression. A group is opened with \u201c\\(\u201d and closed with \u201c\\)\u201d.Grouping can be used in combination with back-referencing.<\/p>\r\n\r\n\r\n\r\n<p>Back-reference is the re-use of a part of a Regular Expression selected by grouping. Back-references in sed can be used in both a Regular Expression and in the replacement part of the substitute command.<\/p>\r\n\r\n\r\n\r\n<p><strong>Example 1: Get only the first path in each line<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ sed 's\/\\(\\\/[^:]*\\).*\/\\1\/g' path.txt\r\n\/usr\/kbos\/bin\r\n\/usr\/local\/sbin\r\n\/opt\/omni\/lbin<\/pre>\r\n\r\n\r\n\r\n<p>In the above example, \\(\\\/[^:]*\\) matches the path available before first: comes. \\1 replaces the first matched group.<\/p>\r\n\r\n\r\n\r\n<p><strong>Example 2: multi grouping<\/strong><\/p>\r\n\r\n\r\n\r\n<p>In the file path.txt change the order of field in the last line of the file.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ sed '$s@\\([^:]*\\):\\([^:]*\\):\\([^:]*\\)@\\3:\\2:\\1@g' path.txt\r\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin:\/usr\/bin:\/usr\/sas\/bin\r\n\/usr\/local\/sbin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\r\n<strong>\/root\/bin:\/opt\/omni\/sbin:\/opt\/omni\/lbin<\/strong><\/pre>\r\n\r\n\r\n\r\n<p>In the above command, $ specifies substitution to happen only for the last line. The output shows that the order of the path values in the last line has been reversed.<\/p>\r\n\r\n\r\n\r\n<p><strong>Example 3: Get the list of usernames in \/etc\/passwd file<\/strong><\/p>\r\n\r\n\r\n\r\n<p>This sed example displays only the first field from the \/etc\/passwd file.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$sed 's\/\\([^:]*\\).*\/\\1\/' \/etc\/passwd\r\nroot\r\nbin\r\ndaemon\r\nadm\r\nlp\r\nsync\r\nshutdown<\/pre>\r\n\r\n\r\n\r\n<p><strong>Example 4: Parenthesize first character of each word<\/strong><\/p>\r\n\r\n\r\n\r\n<p>This sed example prints the first character of every word in parenthesis.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ echo \"Welcome To The Geek Stuff\" | sed 's\/\\(\\b[A-Z]\\)\/\\(\\1\\)\/g'\r\n(W)elcome (T)o (T)he (G)eek (S)tuff<\/pre>\r\n\r\n\r\n\r\n<p><strong>Example 5: Commify the simple number.<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Let us create a file called numbers which has a list of numbers. The below sed command example is used to commify the numbers to thousands.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">$ cat  numbers\r\n1234\r\n12121\r\n3434\r\n123\r\n\r\n$sed 's\/\\(^\\|[^0-9.]\\)\\([0-9]\\+\\)\\([0-9]\\{3\\}\\)\/\\1\\2,\\3\/g' numbers\r\n1,234\r\n12,121\r\n3,434\r\n123<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>In this article, let us review some interesting workarounds with the \u201cs\u201d substitute command in sed with several practical examples. I. Sed Substitution Delimiter In the sed substitute command, users can use different delimiters like, @ % | ; : We first need to create the path.txt file which is used in all the given [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,3],"tags":[170,171,169],"class_list":["post-715","post","type-post","status-publish","format-standard","hentry","category-linux","category-web-hosting","tag-sed-substitution","tag-substitute-command-in-sed","tag-unix-server"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/posts\/715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/comments?post=715"}],"version-history":[{"count":16,"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/posts\/715\/revisions"}],"predecessor-version":[{"id":5596,"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/posts\/715\/revisions\/5596"}],"wp:attachment":[{"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/media?parent=715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/categories?post=715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bodhost.com\/blog\/wp-json\/wp\/v2\/tags?post=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}