{"id":1621,"date":"2017-02-10T07:56:57","date_gmt":"2017-02-10T07:56:57","guid":{"rendered":"http:\/\/www.fastwebhost.in\/blog\/?p=1621"},"modified":"2019-01-22T09:35:46","modified_gmt":"2019-01-22T09:35:46","slug":"how-to-use-sed-command-in-unix-with-examples","status":"publish","type":"post","link":"https:\/\/www.fastwebhost.in\/blog\/how-to-use-sed-command-in-unix-with-examples\/","title":{"rendered":"How to Use SED Command in UNIX with Examples?"},"content":{"rendered":"<h5>What is SED ?<\/h5>\n<p>The SED command in UNIX stands for stream editor, which is used to make changes to file content. In Linux we can simply call it as text editor operator. We will discuss, how to search the strings in a file, update the content of the file, removing the content from the file and replacing the strings in the file.<\/p>\n<h5>The workflow of SED command in UNIX<\/h5>\n<ul>\n<li>Read a line from input stream<\/li>\n<\/ul>\n<ul>\n<li>Execute the commands on a line<\/li>\n<\/ul>\n<ul>\n<li>Display result on output stream<\/li>\n<\/ul>\n<h6>Basic &#8220;Options&#8221; for SED Command in UNIX<\/h6>\n<h6>Options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Functionality<\/h6>\n<blockquote style=\"margin-left: -10px !important;\"><p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -i&nbsp;<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; source file can be edited<\/p><\/blockquote>\n<blockquote style=\"margin-left: -10px !important;\"><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span>-e&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>To run multiple sed commands in a single sed command<\/p><\/blockquote>\n<blockquote style=\"margin-left: -10px !important;\"><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span>-n<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Suppresses default output<\/p><\/blockquote>\n<blockquote style=\"margin-right: -10px !important; margin-left: -10px !important;\"><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span>-f<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Precedes a sed script filename<\/p><\/blockquote>\n<h6>Few basic flags for SED Command in UNIX<\/h6>\n<h6 style=\"margin-left: 20px !important;\">Flags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Functionality<\/h6>\n<blockquote style=\"margin-left: 20px !important;\"><p><span>g&nbsp;&nbsp;&nbsp;<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; Global replacement of a string in file<\/p><\/blockquote>\n<blockquote style=\"margin-left: 20px !important;\"><p><span>i&nbsp;&nbsp;<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ignore case sensitive with substitution<\/p><\/blockquote>\n<blockquote style=\"margin-left: 20px !important;\"><p><span>p<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prints the replaced line on the terminal<\/p><\/blockquote>\n<blockquote style=\"margin-left: 20px !important;\"><p><span>w&nbsp;&nbsp;<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Writes lines out to a file<\/p><\/blockquote>\n<blockquote style=\"margin-left: 20px !important;\"><p><span>x<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; Exchanges contents of the holding buffer with the pattern space<\/p><\/blockquote>\n<blockquote style=\"margin-left: 20px !important;\"><p><span>y<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; Convert the lower case letters to upper case letters<\/p><\/blockquote>\n<p><span>Some special characters of SED command<\/span><\/p>\n<p style=\"margin-left: 20px !important;\"><span>Character&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Description<\/span><\/p>\n<p style=\"margin-left: 20px !important;\">^&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Matches the begining of lines<\/p>\n<p style=\"margin-left: 20px !important;\">$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Matches the end of line<\/p>\n<p style=\"margin-left: 20px !important;\">.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Matches any single character<\/p>\n<p style=\"margin-left: 20px !important;\">*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Matches zero or more occurrences of the previous character<\/p>\n<h6>Let us take a file called &#8220;sample.txt&#8221; and&nbsp;work with different examples<\/h6>\n<p>cat sample.txtUnix is a great OS. Unix is opensource.The Unix is free OS. Learn operating system.Linux or Unix which on you choose.<\/p>\n<p>We can find and replace strings or patterns without opening a file. If we are working with large files which can take a long time to open in Vi editor and take a time to replace existing strings with new strings.We can easily replace the strings in a file using the following SED command without opening file.<\/p>\n<p>sed &#8216;s\/unix\/linux\/&#8217; sample.txt<\/p>\n<p>In the above SED command, first, it performs the search operation for Unix string and replaces with \u2018Linux\u2019 for the first occurrence only.Here the \u201cs\u201d specifies the substitution operation. The \u201c\/\u201d are delimiters.<\/p>\n<p>By default, the sed command replaces the first occurrence of a pattern in each and it won\u2019t replace the second, third..occurrence in the line.<\/p>\n<p>If we use the \u2018g\u2019 flag along with the above command then SED command will replace all unix string with linux.<\/p>\n<p>sed &#8216;s\/unix\/linux\/g&#8217; sample.txt<\/p>\n<h6>SED command with \u2018d\u2019 flag used to delete the lines from the files.<\/h6>\n<p>1. SED command to delete the First line from the file,<\/p>\n<p>sed &#8216;1d&#8217; sample.txt<\/p>\n<p>2. SED command to delete the Last line from the file<\/p>\n<p>sed &#8216;$d&#8217; sample.txt<\/p>\n<p>3. SED command to delete the lines from 1,3 in a file<\/p>\n<p>sed &#8216;1,3d&#8217; sample.txt<\/p>\n<p>4. SED command to remove blank lines from a file<\/p>\n<p>sed &#8216;\/^$\/d&#8217; sample.txt<\/p>\n<p>Here \u201c^\u201d symbol represents the starting point of a line and \u201c$\u201d represents end of the line. Whereas \u201c^$\u201d represents the empty lines.<\/p>\n<h6>Following command with &#8216;p&#8217; flag to print output to the console<\/h6>\n<p>5. We can also print the content of a file using the SED command.<\/p>\n<p>sed -n &#8216;10,20p&#8217; filename.txt<\/p>\n<p>Using the above command we can filter the content of a file to the desktop from 10th line to 20.<\/p>\n<p>6. Similarly, if you want to print from 10 to the end of line you can use the following command.<\/p>\n<p>sed -n &#8217;10,$p&#8217; filename.txt<\/p>\n<p>This is especially useful if we are dealing with a large file. Sometimes you just want to extract a sample without opening the entire file.<\/p>\n<p>7. Command to print a specific line from a file. Here we can print 4th line.<\/p>\n<p>sed -n &#8216;4p&#8217; filename.txt<\/p>\n<p>8. SED command by default does not edit the original or source file for our safety but by using \u2018-i\u2019 option source file can be edited.<\/p>\n<p>sed -i &#8216;1d&#8217; filename.txt<\/p>\n<p>The above command will delete the First line from the file.<\/p>\n<h6>Few SED examples with different options<\/h6>\n<p>9. We can run multiple SED commands by using -e option, as shown in the below example<\/p>\n<p>sed -e &#8216;s\/unix\/linux\/&#8217; -e &#8216;s\/testing\/testingSED\/&#8217; sample.txt<\/p>\n<p>10. We can replace a string in multiple range of lines of a file. From the below example,the string will replace in 2nd and 3rd lines.<\/p>\n<p>sed &#8216;2,3 s\/unix\/linux\/&#8217; sample.txt<\/p>\n<p>11. Using SED command, we can print each line of a file two times by adding &#8216;p&#8217; print flag.<\/p>\n<p>sed &#8216;p&#8217; sample.txt<\/p>\n<p>12. We can take the backup of files using SED command. Using the following command we take the backup of &#8216;sample&#8217; file.<\/p>\n<p>sed -i.bk &#8216;s\/(name)\/sample&#8217; sample.txt<\/p>\n<p>There is much more to learn about SED command in UNIX. I hope you can spend some time to look them up. Hope fully this introduction with few basic commands will create interest and help you learn more about the command line stream editing. We will be updating new examples on SED command regularly. I recommend you to browse this post regularly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is SED ? The SED command in UNIX stands for stream editor, which is used to make changes to file content. In Linux we can simply call it as text editor operator. We will discuss, how to search the strings in a file, update the content of the file, removing the content from the [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":1625,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[22],"tags":[76,75],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/1621"}],"collection":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/comments?post=1621"}],"version-history":[{"count":3,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/1621\/revisions"}],"predecessor-version":[{"id":4692,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/1621\/revisions\/4692"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media\/1625"}],"wp:attachment":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media?parent=1621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/categories?post=1621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/tags?post=1621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}