{"id":2118,"date":"2015-03-11T10:42:41","date_gmt":"2015-03-11T10:42:41","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=2118"},"modified":"2015-03-11T10:53:04","modified_gmt":"2015-03-11T10:53:04","slug":"sed-20-examples-to-remove-delete-characters-from-a-file","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=2118","title":{"rendered":"sed &#8211; 20 examples to remove \/ delete characters from a file"},"content":{"rendered":"<p class=\"post-title entry-title\">In this article of\u00a0<a style=\"font-family: 'PT Serif', sans-serif; font-size: 20px; line-height: 1.5;\" href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\">sed series<\/a>, we will see the examples of how to remove or delete characters from a file. The syntax of sed command replacement is:<\/p>\n<div id=\"post-body-4491733866265148704\" class=\"post-body entry-content\">\n<pre class=\"gpr1\">$ sed 's\/find\/replace\/' file\r\n<\/pre>\n<p>This sed command finds the pattern and replaces with another pattern. When the replace is left empty, the pattern\/element found gets deleted.<\/p>\n<p>Let us consider a sample file as below:<\/p>\n<p class=\"gpr1\">$ cat file Linux Solaris Ubuntu Fedora RedHat<\/p>\n<p><b>1<\/b>.\u00a0<b>To remove a specific character<\/b>, say &#8216;a&#8217;<\/p>\n<pre class=\"gpr1\">$ sed 's\/a\/\/' file\r\nLinux\r\nSolris\r\nUbuntu\r\nFedor\r\nRedHt\r\n<\/pre>\n<p>This will remove the first occurence of &#8216;a&#8217; in every line of the file. To remove all occurences of &#8216;a&#8217; in every line,<\/p>\n<pre class=\"gpr1\">$ sed 's\/a\/\/g' file\r\n<\/pre>\n<p><b>2<\/b>.\u00a0<b>To remove 1st character in every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/^.\/\/' file\r\ninux\r\nolaris\r\nbuntu\r\nedora\r\nedHat\r\n<\/pre>\n<p>.(dot) tries to match a single character. The \u00a0^ tries to match a pattern(any character) in the beginning of the line. \u00a0 Another way to write the same:<\/p>\n<pre class=\"gpr1\">$ sed 's\/.\/\/' file\r\n<\/pre>\n<p>This tells to replace a character with nothing. Since by default, sed starts from beginning, it replaces only the 1st character since &#8216;g&#8217; is not passed.<\/p>\n<p><b>3. To remove last character of every line<\/b>\u00a0:<\/p>\n<pre class=\"gpr1\">$ sed 's\/.$\/\/' file\r\nLinu\r\nSolari\r\nUbunt\r\nFedor\r\nRedHa\r\n<\/pre>\n<p>The $ tries to match a pattern in the end of the line.<\/p>\n<p><b>4. To remove the 1st and last character of every line in the same command<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/.\/\/;s\/.$\/\/' file\r\ninu\r\nolari\r\nbunt\r\nedor\r\nedHa\r\n<\/pre>\n<p>Two commands can be given together with a semi-colon separated in between.<\/p>\n<p><b>5. To remove first character only if it is a specific character<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/^F\/\/' file\r\nLinux\r\nSolaris\r\nUbuntu\r\nedora\r\nRedHat\r\n<\/pre>\n<p>This removes the 1st character only if it is &#8216;F&#8217;.<\/p>\n<p><b>6. To remove last character only if it is a specific character<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/x$\/\/' file\r\nLinu\r\nSolaris\r\nUbuntu\r\nFedora\r\nRedHat\r\n<\/pre>\n<p>This removed the last character only if it s &#8216;x&#8217;.<\/p>\n<p><b>7. To remove 1st 3 characters of every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/...\/\/' file\r\nux\r\naris\r\nntu\r\nora\r\nHat\r\n<\/pre>\n<p>A single dot(.) removes 1st character, 3 dots remove 1st three characters.<\/p>\n<p><b>8. To remove 1st n characters of every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed -r 's\/.{4}\/\/' file\r\nx\r\nris\r\ntu\r\nra\r\nat\r\n<\/pre>\n<p>.{n} -&gt; matches any character n times, and hence the above expression matches 4 characters and deletes it.<\/p>\n<p><b>9. To remove last n characters of every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed -r 's\/.{3}$\/\/' file\r\nLi\r\nSola\r\nUbu\r\nFed\r\nRed\r\n<\/pre>\n<p><b>10. To remove everything except the 1st n characters in every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed -r 's\/(.{3}).*\/\\1\/' file\r\nLin\r\nSol\r\nUbu\r\nFed\r\nRed\r\n<\/pre>\n<p>.* -&gt; matches any number of characters, and the first 3 characters matched are grouped using parantheses. In the replacement, by having \\1 only the group is retained, leaving out the remaining part.<\/p>\n<p><b>11. To remove everything except the last n characters in a file<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed -r 's\/.*(.{3})\/\\1\/' file\r\nnux\r\nris\r\nntu\r\nora\r\nHat\r\n<\/pre>\n<p>Same as last example, except that from the end.<\/p>\n<p><b>12. To remove multiple characters present in a file<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/[aoe]\/\/g' file\r\nLinux\r\nSlris\r\nUbuntu\r\nFdr\r\nRdHt\r\n<\/pre>\n<p>To delete multiple characters, [] is used by specifying the characters to be removed. This will remove all occurences of the characters a, o and e.<\/p>\n<p><b>13. To remove a pattern \u00a0<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/lari\/\/g' file\r\nLinux\r\nSos\r\nUbuntu\r\nFedora\r\nRedHat\r\n<\/pre>\n<p>Not just a character, even a pattern can be removed. Here, &#8216;lari&#8217; got removed from &#8216;Solaris&#8217;.<\/p>\n<p><b>14. To delete only nth occurrence of a character in every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/u\/\/2' file\r\nLinux\r\nSolaris\r\nUbunt\r\nFedora\r\nRedHat\r\n<\/pre>\n<p>By default, sed performs an activity only on the 1st occurence. If n is specifed, sed performs only on the nth occurence of the pattern. The 2nd &#8216;u&#8217; of &#8216;Ubuntu&#8217; got deleted.<\/p>\n<p><b>15. To delete everything in a line followed by a character<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/a.*\/\/' file\r\nLinux\r\nSol\r\nUbuntu\r\nFedor\r\nRedH\r\n<\/pre>\n<p><b>16. To remove all digits present in every line of a file<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/[0-9]\/\/g' file\r\n<\/pre>\n<p>[0-9] stands for all characters between 0 to 9 meaning all digits, and hence all digits get removed.<\/p>\n<p><b>17. To remove all lower case alphabets present in every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/[a-z]\/\/g' file\r\nL\r\nS\r\nU\r\nF\r\nRH\r\n<\/pre>\n<p>[a-z] represents lower case alphabets range and hence all lower-case characters get removed.<\/p>\n<p><b>18. To remove everything other than the lower case alphabets<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/[^a-z]\/\/g' file\r\ninux\r\nolaris\r\nbuntu\r\nedora\r\nedat\r\n<\/pre>\n<p>^ inside square brackets negates the condition. Here, all characters except lower case alphabets get removed.<\/p>\n<p><b>19. To remove all alpha-numeric characters present in every line<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/[a-zA-Z0-9]\/\/g' file\r\n<\/pre>\n<p>All alpha-numeric characters get removed.<\/p>\n<p><b>20. To remove a character irrespective of the case<\/b>:<\/p>\n<pre class=\"gpr1\">$ sed 's\/[uU]\/\/g' file\r\nLinx\r\nSolaris\r\nbnt\r\nFedora\r\nRedHat\r\n<\/pre>\n<p>By specifying both the lower and upper case character in brackets is equivalent to removing a character irrespective of the case.<\/p>\n<\/div>\n<p>&#8211; See more at: http:\/\/www.theunixschool.com\/2014\/08\/sed-examples-remove-delete-chars-from-line-file.html#sthash.bOu9cfRg.dpuf<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article of\u00a0sed series, we will see the examples of how to remove or delete characters from a file. The syntax [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2118","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2118"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2118\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}