{"id":832,"date":"2015-05-20T08:38:50","date_gmt":"2015-05-20T17:38:50","guid":{"rendered":"http:\/\/blog.box.kr\/?p=832"},"modified":"2015-05-20T08:38:50","modified_gmt":"2015-05-20T17:38:50","slug":"scrapunix-sed-tutorial-advanced-sed-substitution-examples","status":"publish","type":"post","link":"https:\/\/blog.box.kr\/?p=832","title":{"rendered":"[scrap]Unix Sed Tutorial: Advanced Sed Substitution Examples"},"content":{"rendered":"<p><a href=\"http:\/\/www.thegeekstuff.com\/2009\/10\/unix-sed-tutorial-advanced-sed-substitution-examples\/\">http:\/\/www.thegeekstuff.com\/2009\/10\/unix-sed-tutorial-advanced-sed-substitution-examples\/<\/a><\/p>\n<p>This article is part of the on-going<a href=\"http:\/\/www.thegeekstuff.com\/tag\/sed-tips-and-tricks\/\">Unix Sed Tips and Tricks<\/a> series.<\/p>\n<p>In our previous sed articles we learned \u2014 <a href=\"http:\/\/www.thegeekstuff.com\/2009\/09\/unix-sed-tutorial-printing-file-lines-using-address-and-patterns\/\">sed printing<\/a>, <a href=\"http:\/\/www.thegeekstuff.com\/2009\/09\/unix-sed-tutorial-delete-file-lines-using-address-and-patterns\/\">sed deletion<\/a>, <a href=\"http:\/\/www.thegeekstuff.com\/2009\/09\/unix-sed-tutorial-replace-text-inside-a-file-using-substitute-command\/\">sed substitute <\/a>, <a href=\"http:\/\/www.thegeekstuff.com\/2009\/10\/unix-sed-tutorial-how-to-write-to-a-file-using-sed\/\">sed file write<\/a>, and <a href=\"http:\/\/www.thegeekstuff.com\/2009\/10\/unix-sed-tutorial-how-to-execute-multiple-sed-commands\/\">sed multiple commands<\/a>.<\/p>\n<p>In this article, let us review some interesting workarounds with the \u201cs\u201d substitute command in sed with several practical examples.<\/p>\n<h2>I. Sed Substitution Delimiter<\/h2>\n<p>As we discussed in our previous post, we can use the different delimiters such as @ % | ; : in sed substitute command.<\/p>\n<p>Let us first create path.txt file that will be used in all the examples mentioned below.<\/p>\n<pre>$ cat path.txt\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin:\/usr\/bin:\/usr\/sas\/bin\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\n\/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin<\/pre>\n<h3>Example 1 \u2013 sed @ delimiter: Substitute \/opt\/omni\/lbin to \/opt\/tools\/bin<\/h3>\n<p>When you substitute a path name which has \u2018\/\u2019, 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>\n<p><center><\/p>\n<div>\n<\/div>\n<p><\/center><\/p>\n<pre>$ sed 's@\/opt\/omni\/lbin@\/opt\/tools\/bin@g' path.txt\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\n<strong>\/opt\/tools\/bin<\/strong>:\/opt\/omni\/sbin:\/root\/bin<\/pre>\n<h3>Example 2 \u2013 sed \/ delimiter: Substitute \/opt\/omni\/lbin to \/opt\/tools\/bin<\/h3>\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>\n<pre>$ sed 's\/\/opt\/omni\/lbin\/\/opt\/tools\/bin\/g' path.txt\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\n<strong>\/opt\/tools\/bin<\/strong>:\/opt\/omni\/sbin:\/root\/bin<\/pre>\n<h2>II. Sed \u2018&amp;\u2019 Get Matched String<\/h2>\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>\n<h3>Example 1 \u2013 sed &amp; Usage: Substitute \/usr\/bin\/ to \/usr\/bin\/local<\/h3>\n<pre>$ sed 's@\/usr\/bin@&amp;\/local@g' path.txt\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:<strong>\/usr\/bin\/local<\/strong>:\/usr\/sas\/bin\n\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:<strong>\/usr\/bin\/local<\/strong>:\/opt\/omni\/bin:\n\/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin<\/pre>\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 occurrance of \/usr\/bin will be replaced with \/usr\/bin\/local<\/p>\n<h3>Example 2 \u2013 sed &amp; Usage: Match the whole line<\/h3>\n<p>&amp; replaces whatever matches with the given REGEXP.<\/p>\n<pre>$ sed 's@^.*$@&lt;&lt;&lt;&amp;&gt;&gt;&gt;@g' path.txt\n&lt;&lt;&lt;\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin&gt;&gt;&gt;\n&lt;&lt;&lt;\/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:&gt;&gt;&gt;\n&lt;&lt;&lt;\/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin&gt;&gt;&gt;<\/pre>\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>\n<h2>III. Grouping and Back-references in Sed<\/h2>\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>\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>\n<h3>Example 1: Get only the first path in each line<\/h3>\n<pre>$ sed 's\/(\/[^:]*).*\/1\/g' path.txt\n\/usr\/kbos\/bin\n\/usr\/local\/sbin\n\/opt\/omni\/lbin<\/pre>\n<p>In the above example, (\/[^:]*) matches the path available before first : comes. 1 replaces the first matched group.<\/p>\n<h3>Example 2: Multigrouping<\/h3>\n<p>In the file path.txt change the order of field in the last line of the file.<\/p>\n<pre>$ sed '$s@([^:]*):([^:]*):([^:]*)@3:2:1@g' path.txt\n\/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin:\/usr\/bin:\/usr\/sas\/bin\n\/usr\/local\/sbin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin:\n<strong>\/root\/bin:\/opt\/omni\/sbin:\/opt\/omni\/lbin<\/strong><\/pre>\n<p>In the above command $ specifies substitution to happen only for the last line.Output shows that the order of the path values in the last line has been reversed.<\/p>\n<h3>Example 3: Get the list of usernames in \/etc\/passwd file<\/h3>\n<p>This sed example displays only the first field from the \/etc\/passwd file.<\/p>\n<pre>$sed 's\/([^:]*).*\/1\/' \/etc\/passwd\nroot\nbin\ndaemon\nadm\nlp\nsync\nshutdown<\/pre>\n<h3>Example 4: Parenthesize first character of each word<\/h3>\n<p>This sed example prints the first character of every word in paranthesis.<\/p>\n<pre>$ echo \"Welcome To The Geek Stuff\" | sed 's\/(b[A-Z])\/(1)\/g'\n(W)elcome (T)o (T)he (G)eek (S)tuff<\/pre>\n<h3>Example 5: Commify the simple number.<\/h3>\n<p>Let us create file called numbers which has list of numbers. The below sed command example is used to commify the numbers till thousands.<\/p>\n<pre>$ cat  numbers\n1234\n12121\n3434\n123\n\n$sed 's\/(^|[^0-9.])([0-9]+)([0-9]{3})\/12,3\/g' numbers\n1,234\n12,121\n3,434\n123<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.thegeekstuff.com\/2009\/10\/unix-sed-tutorial-advanced-sed-substitution-examples\/ This article is part of the on-goingUnix Sed Tips and Tricks series. In our previous sed articles we learned \u2014 sed printing, sed deletion, sed substitute , sed file write, and sed multiple commands. 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 As we discussed in our previous post, we can use the different delimiters such as @ % | ; : in sed substitute command. Let us first create path.txt file that will be used in all the examples mentioned below. $ cat path.txt \/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin:\/usr\/bin:\/usr\/sas\/bin \/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin: \/opt\/omni\/lbin:\/opt\/omni\/sbin:\/root\/bin Example 1 \u2013 sed @ delimiter: Substitute \/opt\/omni\/lbin to \/opt\/tools\/bin When you substitute a path name which has \u2018\/\u2019, 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. $ sed &#8216;s@\/opt\/omni\/lbin@\/opt\/tools\/bin@g&#8217; path.txt \/usr\/kbos\/bin:\/usr\/local\/bin:\/usr\/jbin\/:\/usr\/bin:\/usr\/sas\/bin \/usr\/local\/sbin:\/sbin:\/bin\/:\/usr\/sbin:\/usr\/bin:\/opt\/omni\/bin: \/opt\/tools\/bin:\/opt\/omni\/sbin:\/root\/bin Example 2 \u2013 sed \/ delimiter: Substitute \/opt\/omni\/lbin to \/opt\/tools\/bin 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"ngg_post_thumbnail":0,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4,5],"tags":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5q9Zn-dq","jetpack-related-posts":[{"id":834,"url":"https:\/\/blog.box.kr\/?p=834","url_meta":{"origin":832,"position":0},"title":"[scrap]awk introduction tutorial 7 awk print examples","date":"2015-05-20","format":false,"excerpt":"http:\/\/www.thegeekstuff.com\/2010\/01\/awk-introduction-tutorial-7-awk-print-examples\/ \u00a0 This is the first article on the new awk tutorial series. We\u2019ll be posting several articles on awk in the upcoming weeks that will cover all features of awk with practical examples. In this article, let us review the fundamental awk working methodology along with 7 practical awk\u2026","rel":"","context":"In &quot;\uae30\uc220&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":830,"url":"https:\/\/blog.box.kr\/?p=830","url_meta":{"origin":832,"position":1},"title":"[scrap]50 Most Frequently Used UNIX \/ Linux Commands (With Examples)","date":"2015-05-20","format":false,"excerpt":"http:\/\/www.thegeekstuff.com\/2010\/11\/50-linux-commands\/ \u00a0 This article provides practical examples for 50 most frequently used commands in Linux \/ UNIX. This is not a comprehensive list by any means, but this should give you a jumpstart on some of the common Linux commands. Bookmark this article for your future reference. Did I miss\u2026","rel":"","context":"In &quot;\uae30\uc220&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":799,"url":"https:\/\/blog.box.kr\/?p=799","url_meta":{"origin":832,"position":2},"title":"[scrap] grep , sed, awk \uc815\uaddc\uc2dd","date":"2015-05-17","format":false,"excerpt":"** grep \ud55c \ub370\uc774\ud130 \uc911 3\ubc88\uc9f8 \ud56d\ubaa9\ub9cc \uac00\uc838 \uc624\uace0 \uc2f6\uc744\ub54c.. grep \".xml\" | awk 'print {$3}' http:\/\/unabated.tistory.com\/447 \uc815\uaddc\uc2dd\uc774\ub780 \ubb34\uc5c7\uc778\uac00 ? \uc5b4\ub5a4 \ubb38\uc790\uc5f4\uc758 \uc9d1\ud569\uc744 \ubb18\uc0ac\ud558\ub294\ub370 \uc0ac\uc6a9\ub418\ub294 \ud14d\uc2a4\ud2b8 \uc2a4\ud2b8\ub9c1 \uc815\ud574\uc9c4 \uad6c\ubb38 \uaddc\uce59\uc5d0 \ub530\ub978\ub2e4 Editor, Utillity, Programming \uc5b8\uc5b4\uc5d0\uc11c \ud14d\uc2a4\ud2b8 \ud328\ud134\uc744 \uae30\uc900\uc73c\ub85c \uac80\uc0c9, \ud639\uc740 \uc870\uc791\ud558\ub294\ub370 \uc0ac\uc6a9\ub41c\ub2e4 \ubcf4\ud1b5 \uc0ac\ub78c\ub4e4\uc774 \uc4f0\ub294 \ud45c\ud604 \uc911\uc5d0\ub3c4 \uadf8\ub300\ub85c \ubb38\uc7a5\uc744 \ud574\uc11d\ud558\uba74\u2026","rel":"","context":"In &quot;Linux&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":815,"url":"https:\/\/blog.box.kr\/?p=815","url_meta":{"origin":832,"position":3},"title":"L(U)nix \uc5d0\uc11c \ud2b9\uc815 \ubb38\uc790\uc5f4 \uc704\uce58 \ud45c\uc2dc \ubc0f \uad6c\uac04 \uc815\ubcf4 \ubcf4\uae30","date":"2015-05-19","format":false,"excerpt":"\u00a0 grep \u00a0 -n \"\ud328\ud134\" \u00a0\ud654\uc77c\uba85 \u00a0\uc5d0\uc11c \ucc98\ub7fc \u00a0-n \uc635\uc158\uc744 \uc0ac\uc6a9\ud558\uba74 \ucc3e\uace0\uc790 \ud558\ub294 \ubb38\uc790\uc5f4\uc774 \ud3ec\ud568\uc774 \ub418\uc5b4 \uc788\ub294 line number\ub97c \ud45c\uc2dc \ud574 \uc900\ub2e4. \u00a0 sed -n 'xxxx,xxxxp' \u00a0\ud654\uc77c\uba85 \u00a0 \uc774\ub807\uac8c \ud558\uba74 \ud654\uc77c\uc5d0\uc11c xxxx\uc5d0\uc11c xxxxp\uae4c\uc9c0\uc758 \ub0b4\uc6a9\uc744 \ucd9c\ub825 \ud574 \uc900\ub2e4. ( p\ub294 \ub4a4\uc5d0\ub9cc \ubd99\uc784 ) ex) xxx.log\uc5d0\uc11c 100\uc904 \uc5d0\uc11c 200\uc904\ub9cc \ud45c\uc2dc \ud558\uae30 =>\u2026","rel":"","context":"In &quot;\uae30\uc220\uc790\ub8cc&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":772,"url":"https:\/\/blog.box.kr\/?p=772","url_meta":{"origin":832,"position":4},"title":"Installing Taiga on CentOS  (x64)","date":"2015-05-11","format":false,"excerpt":"\u00a0 Dependencies ... yum update -y yum groupinstall \"Development Tools\" -y yum install libxslt-devel libxml2-devel libXt-devel curl git tmux -y Installing PostgreSQL ... TAIGA_POSTGRES_BD = taiga TAIGA_POSTGRES_USER = taiga TAIGA_POSTGRES_PASSWORD=`< \/dev\/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-20};echo;` ... rpm -ivh http:\/\/yum.postgresql.org\/9.4\/redhat\/rhel-6-x86_64\/pgdg-centos94-9.4-1.noarch.rpm sed -i 's\/^gpgkey.*\/&nexclude=postgresql*\/' \/etc\/yum.repos.d\/CentOS-Base.repo yum -y install postgresql94 postgresql94-contrib\u2026","rel":"","context":"In &quot;\uae30\uc220\uc790\ub8cc&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":826,"url":"https:\/\/blog.box.kr\/?p=826","url_meta":{"origin":832,"position":5},"title":"[scrap]50 UNIX \/ Linux Sysadmin Tutorials","date":"2015-05-20","format":false,"excerpt":"http:\/\/www.thegeekstuff.com\/2010\/12\/50-unix-linux-sysadmin-tutorials\/ \u00a0 Merry Christmas and Happy Holidays to all TGS Readers. To wrap this year, I\u2019ve collected 50 UNIX \/ Linux sysadmin related tutorials that we\u2019ve posted so far. This is lot of reading. Bookmark this article for your future reference and read it whenever you get free time. Disk\u2026","rel":"","context":"In &quot;\uae30\uc220&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/832"}],"collection":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=832"}],"version-history":[{"count":0,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/832\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}