

{"id":110397,"date":"2022-07-26T09:00:55","date_gmt":"2022-07-26T03:30:55","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=110397"},"modified":"2022-07-26T10:34:02","modified_gmt":"2022-07-26T05:04:02","slug":"sed-command-in-linux","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/","title":{"rendered":"Sed Command in Linux"},"content":{"rendered":"<p>In this article, you will learn all there is to the SED command in Linux-based operating systems. We will be going through, what SED is, how it works, the syntax, and the options of the SED command. We will also be looking into some practical examples of the command in the terminal.<\/p>\n<h3>What is SED Command in Linux?<\/h3>\n<p>SED is an abbreviation for \u201cStream Editor\u201d. It is a command-line-based utility in Linux-based operating systems that helps in editing streams. It may sound simple when you say \u2018editing\u2019, but what it really does is searching, finding, replacing, sorting, inserting, and deleting, now it sounds fun!<\/p>\n<p>The sed command can either be used by piping it with some other main command or simply using it on a text file. When we talk about \u201cediting\u201d using sed, it is done using commands only. Sed does not have an interactive text editor interface like nano, cat, or vi, you have to do all the process of editing solely using commands.<\/p>\n<p>Using sed we can select the text, delete the line from the text, substitute text (find and replace) modify, and delete text. The best part about sed is, you don\u2019t even have to open files to edit them (unlike vi and nano), thus making the workflow easier and efficient. Before we go any further, let us look at 1 really simple example of sed to take in the unorthodox text editor because sed is not your average text editor, it&#8217;s far better!<\/p>\n<p>Let us take the simple command: echo DataFair | sed -e \u2018s\/Fair\/Flair\/\u2019<\/p>\n<p>&nbsp;<\/p>\n<p>This command will give the following output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/applying-the-sed-command-to-standard-input.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110463\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/applying-the-sed-command-to-standard-input.webp\" alt=\"applying the sed command to standard input\" width=\"716\" height=\"80\" \/><\/a><\/p>\n<p>As you saw, we found and replace the string pattern \u201cFair\u201d with \u201cFlair\u201d.<\/p>\n<h3>How does the SED command work?<\/h3>\n<p>Sed contains 2 data buffers that are both initially empty, they are pattern space and auxiliary hold space. When sed is invoked, it read one line from the input text, (removes trailing lines if any), and stores it in the pattern space. Then the commands are executed, and each command has an address associated with it.<\/p>\n<p>When sed reaches the end of the script, the contents of the pattern space are printed out to the output stream. And when it prints out the output, it also adds back the trailing lines which were removed earlier.<\/p>\n<h3>Syntax of sed command in linux<\/h3>\n<p>The syntax of the sed command may look slightly intimidating at first, but when we discuss what the fields re and why they are used, you will get the hang of it, The syntax of the sed is command is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed &lt;options&gt; &lt;script&gt; &lt;input file&gt;<\/pre>\n<p>Let us look at the fields one by one:<\/p>\n<h4>1. Options:<\/h4>\n<p>This field takes in options that specify how sed should function.<\/p>\n<h4>2. Script<\/h4>\n<p>This field specifies what editing should be done on the file.<\/p>\n<h4>3. Input file<\/h4>\n<p>This field takes in the file that has to be edited.<\/p>\n<h3>Options used with Linux SED command<\/h3>\n<p>As we saw, the options file in the syntax of the sed command takes in a range of options that specifies how the sed command should work and how the output should be formatted. Let us look at the options available with the SED command.<\/p>\n<h4>1. -n<\/h4>\n<p>This option suppresses the automatic printing of pattern space. You can also write this option as \u201c&#8211;quite\u201d or \u201c&#8211;silent\u201d.<\/p>\n<h4>2. -e &lt;script&gt;<\/h4>\n<p>This option adds the script to the command you want to execute. You can also write this command as \u201c&#8211;expression &lt;script&gt;\u201d<\/p>\n<h4>3. -f &lt;script-file&gt;<\/h4>\n<p>This option adds the contents of the script file to the commands to be executed. You can also write this command as \u201c&#8211;file &lt;script file&gt;\u201d<\/p>\n<h4>4. &#8211;follow-symlinks<\/h4>\n<p>This option follows symlinks when processing is happening.<\/p>\n<h4>5. -i &lt;SUFFIX&gt;<\/h4>\n<p>This option edits files in place. You can also write this command as \u201c&#8211;in-place &lt;SUFFIX&gt;\u201d<\/p>\n<h4>6. &#8211;help<\/h4>\n<p>This option displays the help menu as shown below<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/help-menu-of-the-sed-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110464\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/help-menu-of-the-sed-command.webp\" alt=\"help menu of the sed command\" width=\"780\" height=\"848\" \/><\/a><\/p>\n<h4>7. &#8211;POSIX<\/h4>\n<p>This option disables all the GNU extensions.<\/p>\n<h4>8. -r<\/h4>\n<p>This option uses extended regular expressions in th script. You can also write this command as \u201c&#8211;regexp-extended\u201d.<\/p>\n<h4>9. -s<\/h4>\n<p>This option considers files as separate rather than as a long single continuous stream. You can also write this command as \u201c&#8211;separate\u201d<\/p>\n<h4>10. -u<\/h4>\n<p>This option Load minimal amounts of data from the input files and flush the output buffers more often. You can also write this option as \u201c&#8211;unbuffered\u201d.<\/p>\n<h4>11. -I &lt;N&gt;<\/h4>\n<p>This option specifies the desired line warp length &lt;N&gt;, you can also write this command as \u201c&#8211;line-length &lt;N&gt;\u201d<\/p>\n<h4>12. &#8211;version<\/h4>\n<p>This option displays information about the version of sed you are using.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/version-of-the-sed-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110465\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/version-of-the-sed-command.webp\" alt=\"version of the sed command\" width=\"780\" height=\"279\" \/><\/a><\/p>\n<p>Now that we have laid down the fundamentals, let us put all this knowledge together and see some practical examples of the sed command in action.<\/p>\n<h3>Selecting a range of lines<\/h3>\n<p>If you want to select lines in a document between a specific range, use the option \u201c-n\u201d followed by the range you want to see as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed -n \u2018&lt;n1&gt;,&lt;n2&gt;p\u2019 &lt;filename&gt;<\/pre>\n<p>Let us consider the following text file:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file-to-use-the-sed-command-on.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110466\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file-to-use-the-sed-command-on.webp\" alt=\"sample text file to use the sed command on\" width=\"780\" height=\"275\" \/><\/a><\/p>\n<p>If I run the command \u201csed -n \u20185,10p\u2019 file.txt\u201d, I will get only the lines from 5 to 10 as shown below:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/selecting-a-range-of-lines.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110467\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/selecting-a-range-of-lines.webp\" alt=\"selecting a range of lines\" width=\"778\" height=\"190\" \/><\/a><\/p>\n<h3>Viewing the entire file except for the selected range<\/h3>\n<p>Let us now try to do the negative of the proviso output, let us print the entire file, except the selected range. To do so, use the same command as used above, without the option \u201c-n\u201d as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018&lt;n1&gt;,&lt;n2&gt;d\u2019 &lt;filename&gt;<\/pre>\n<p>For example, the command \u201csed \u20185,10d\u2019 file.txt\u201d, will print the entire file except for the line from 5-10.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/viewing-the-entire-file-except-for-the-selected-range.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110468\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/viewing-the-entire-file-except-for-the-selected-range.webp\" alt=\"viewing the entire file except for the selected range\" width=\"780\" height=\"160\" \/><\/a><\/p>\n<h3>Viewing nonconsecutive lines and ranges<\/h3>\n<p>You can also choose to display nonconsecutive lines of the file. For example, if you want to display the lines from 2-6 and 9-11, use the command as shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed -n -e \u20182,6p\u2019 -e \u20189,11p\u2019 file.txt<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/viewing-nonconsecutive-lines-and-ranges.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110469\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/viewing-nonconsecutive-lines-and-ranges.webp\" alt=\"viewing nonconsecutive lines and ranges\" width=\"780\" height=\"207\" \/><\/a><\/p>\n<h3>Replacing words and characters<\/h3>\n<p>To replace every occurrence of a specific word or character, use the following syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018s\/&lt;old string&gt;\/&lt;new string&gt;\/g\u2019 &lt;filename&gt;<\/pre>\n<p>For example, If I want to replace the word \u201cDebian\u201d with \u201cUbuntu\u201d, I will use the command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018s\/&lt;Debian&gt;\/&lt;Ubuntu&gt;\/g\u2019 file.txt<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-and-characters.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110471\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-and-characters.webp\" alt=\"replacing words and characters\" width=\"780\" height=\"278\" \/><\/a><\/p>\n<p>Notice that when I specified \u201cDebian\u201d I specified it exactly how it was (notice th case). However, if you want to ignore the case of the word, use \u201cgi\u201d instead of \u201cg\u201d.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-and-characters-by-making-sed-case-insensitive.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110470\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-and-characters-by-making-sed-case-insensitive.webp\" alt=\"replacing words and characters by making sed case insensitive\" width=\"780\" height=\"511\" \/><\/a><\/p>\n<p>Notice that when I used \u201cDebian\u201d with \u201cg\u201d, it did not replace it, however, when I used \u201cgi\u201d, it was successfully replaced.<\/p>\n<h3>Replacing words only within a specific range<\/h3>\n<p>If you want to replace words only in a specific range and not though out the file, you can use the syntax shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018&lt;n1&gt;,&lt;n2&gt; s\/&lt;old string&gt;\/&lt;new string&gt;\/g\u2019 &lt;filename&gt;<\/pre>\n<p>For example, If I want to replace the name \u201cGNU\u201d with \u201cGPL\u201d only in the range 9-11, I use the command shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u20189,11 s\/GNUGPL\/g\u2019 file.txt<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-only-within-a-specific-range.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110472\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-only-within-a-specific-range.webp\" alt=\"replacing words only within a specific range\" width=\"780\" height=\"279\" \/><\/a><\/p>\n<p>In the above output, the text \u201cGNU\u201d in the 9th and 10th lines has been changed (marked in blue), but the word \u201cGNU\u201d in the 4th line remained unchanged (marked in yellow).<\/p>\n<h3>Substituting using regular expressions<\/h3>\n<p>We all know that all the configuration files have comments in them, what if we wanted to read only the comments and not any other content? Well we cal make use of regular expression and filter out the lines starting with a particular character.<\/p>\n<p>We know that all the comments start with a hash (#) so we can replace the hash character with a dollar sign ($) to count them as blank lines.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/substituting-using-regular-expressions.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110473\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/substituting-using-regular-expressions.webp\" alt=\"substituting using regular expressions\" width=\"626\" height=\"216\" \/><\/a><\/p>\n<h3>Viewing lines that contain a specified pattern<\/h3>\n<p>If you want to print the lines that contain the pattern you specify, use the following syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed -n \u2018\/^&lt;pattern&gt;\/ p\u2019 &lt;filename&gt;<\/pre>\n<p>For example, if you want to display the line that starts with the word \u201cCopyright\u201d, you can use the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed -n \u2018\/^Copyright\/ p\u2019 file.txt<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/viewing-lines-that-contain-a-specified-pattern.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110474\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/viewing-lines-that-contain-a-specified-pattern.webp\" alt=\"viewing lines that contain a specified pattern\" width=\"722\" height=\"92\" \/><\/a><\/h3>\n<h3>Inserting space in files<\/h3>\n<p>We can also insert blank lines between the content using the sed command. To insert a single blank line, use the option \u201cG\u201d and to insert 2 blank lines, use the option \u201cG;G\u201d.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/inserting-space-in-files.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110475\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/inserting-space-in-files.webp\" alt=\"inserting space in files\" width=\"780\" height=\"514\" \/><\/a><\/p>\n<h3>In-place editing and backing up a file.<\/h3>\n<p>Using the sed command, you can also create a backup and edit the file in one single command.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/in-place-editing-and-backing-up-a-file..webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110476\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/in-place-editing-and-backing-up-a-file..webp\" alt=\"in place editing and backing up a fil .\" width=\"752\" height=\"162\" \/><\/a><\/p>\n<p>In the above output, I created a back u file and replaced the word \u201cDebian\u201d with \u201cRHEL\u201d.<\/p>\n<h3>Switching a pair of words<\/h3>\n<p>Say that you have a file where you may want to exchange the position of 2 words, you can do so, by using the syntax shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018s\/^\\(.*\\),\\(.*\\)$\/\\, \\g\u2019 &lt;filename&gt;<\/pre>\n<p>For example, consider the following text file:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file-to-switch-pair-of-words.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110477\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file-to-switch-pair-of-words.webp\" alt=\"sample text file to switch pair of words\" width=\"515\" height=\"221\" \/><\/a><\/p>\n<p>Upon running the command \u201csed \u2018s\/^\\(.*\\),\\(.*\\)$\/\\, \\g\u2019 expression.txt\u201d, will give the following output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/switching-a-pair-of-words-using-the-sed-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110479\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/switching-a-pair-of-words-using-the-sed-command.webp\" alt=\"switching a pair of words using the sed command\" width=\"780\" height=\"196\" \/><\/a><\/p>\n<h3>Replacing words only if a separate match is found<\/h3>\n<p>We can take substitutions using the SED command to the next level, by fixing specific conditions, we can replace a word if and only if another specified word exists in the same sentence.<\/p>\n<p>For example, if you want to replace the word \u201cGNU\u201d with License if and only if the word \u201cGPL\u201d exists in the sentence.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-only-if-a-separate-match-is-found.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110480\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/replacing-words-only-if-a-separate-match-is-found.webp\" alt=\"replacing words only if a separate match is found\" width=\"780\" height=\"487\" \/><\/a><\/p>\n<h3>Substitution 2 or more words at once<\/h3>\n<p>You can take substitution even further, by replacing 2 or more words in just one command. This is not as anticipated as it sounds. You just write the same syntax of replacing strings one after the other separated by a semicolon (;).<\/p>\n<p>For example, If I want to replace the text \u201cDebian\u201d with \u201cRHEL\u201d and \u201cGNU\u201d with \u201cGPL\u201d, in just one command, I used the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed -i \u2018s\/Debian\/RHEL\/g\u2019;\u2019s\/GNU\/GPL\/gi\u2019 file.txt<\/pre>\n<h3>Combining sed with other commands<\/h3>\n<p>In the very first section itself, we say that sed could be combined with other commands. In fact, we can combine one sed command with another sed command. All this connecting can simply be done by using pipes (|). Here is an example:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/combining-sed-with-other-commands.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110481\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/combining-sed-with-other-commands.webp\" alt=\"combining sed with other commands\" width=\"780\" height=\"229\" \/><\/a><\/p>\n<h3>Printing line numbers<\/h3>\n<p>To print the line numbers of a file, use \u2018=\u2019 as shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018=\u2019 &lt;filename&gt;<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/printing-line-numbers.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110482\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/printing-line-numbers.webp\" alt=\"printing line numbers\" width=\"658\" height=\"428\" \/><\/a><\/p>\n<p>If you want to know the line numbers of a specific word, you can use the command: sed -n \u2018\/&lt;word&gt;\/=\u2019 &lt;filename&gt;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/printing-line-numbers-of-a-specific-word.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110483\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/printing-line-numbers-of-a-specific-word.webp\" alt=\"printing line numbers of a specific word\" width=\"560\" height=\"145\" \/><\/a><\/p>\n<h3>Modifying lines<\/h3>\n<p>To modify a specific line, use the option \u201cc\u201d as shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018&lt;n&gt;c\\&lt;sentence&gt;\u2019 &lt;filename&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/modifying-lines.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110484\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/modifying-lines.webp\" alt=\"modifying lines\" width=\"780\" height=\"523\" \/><\/a><\/h3>\n<h3>Inserting and appending text<\/h3>\n<p>We saw that the flags \u201ci\u201d and \u201ca\u201d are for inserting text, however, the key difference between the 2 is that \u201ci\u201d inserts before the text, and \u201ca\u201d inserts after the text.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/inserting-and-appending-text.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110485\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/inserting-and-appending-text.webp\" alt=\"inserting and appending text\" width=\"705\" height=\"185\" \/><\/a><\/p>\n<h3>Removing a line<\/h3>\n<p>To remove a line from a file, use the flag \u201cd\u201d as shown below:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/removing-a-line.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110486\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/removing-a-line.webp\" alt=\"removing a line\" width=\"780\" height=\"448\" \/><\/a><\/p>\n<h3>Parenthesize the first character of each word<\/h3>\n<p>Use the command shown below to enclose the first cahectar pof each owrd in a parentheses:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">echo &lt;string&gt; | sed 's\/\\(\\b[A-Z]\\)\/\\(\\1\\)\/g'<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/parenthesize-the-first-character-of-each-word.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110487\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/parenthesize-the-first-character-of-each-word.webp\" alt=\"parenthesize the first character of each word\" width=\"1201\" height=\"75\" \/><\/a><\/h3>\n<h3>Deleting the last line<\/h3>\n<p>To delete the last line of the file make use of the option \u201cd\u201d as shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed \u2018$d\u2019 &lt;filename&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/deleting-the-last-line.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110488\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/deleting-the-last-line.webp\" alt=\"deleting the last line\" width=\"780\" height=\"501\" \/><\/a><\/h3>\n<h3>Deleting lines in a range<\/h3>\n<p>To delete lines between a specific range, use the option \u201cd\u201d, prefixed by the range as shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sed '&lt;n1&gt;,&lt;n2&gt;d' &lt;filename&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/deleting-lines-in-a-range.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110489\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/deleting-lines-in-a-range.webp\" alt=\"deleting lines in a range\" width=\"780\" height=\"428\" \/><\/a><\/h3>\n<h3>Deleting from the nth line to the last line<\/h3>\n<p>To delete the set of lines from a specific line all the way to the end, use the option \u201cd\u201d prefixed by the line number you want to delete from as shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ sed '&lt;n&gt;,$d' &lt;filename&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/deleting-from-the-nth-line-to-th-last-line.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110490\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/deleting-from-the-nth-line-to-th-last-line.webp\" alt=\"deleting from the nth line to th last line\" width=\"780\" height=\"118\" \/><\/a><\/h3>\n<h3>Using addresses<\/h3>\n<p>The sed command also support addresses, you can specify the address of a file rather that going into that directory and writing its name, here is an example:<\/p>\n<p>&nbsp;<\/p>\n<h3>Summary<\/h3>\n<p>As you have seen, the sed command is not your traditional text\/stream editor, I would reckon it is far better! Even though the changes you make to the file are not permanent, it gives you infinite possibilities of editing streams and text, as we have seen with 25 beautiful examples.<\/p>\n<p>You have now learned what sed is, why we use it, how it works, the syntax, options used with the command, and 25 wonderful examples of the sed command in action.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn all there is to the SED command in Linux-based operating systems. We will be going through, what SED is, how it works, the syntax, and the options of&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":110461,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[27060],"class_list":["post-110397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-sed-command-in-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sed Command in Linux - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what SED command in linux is, how it works, syntax, and options of the SED command and practical examples of its uses.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sed Command in Linux - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what SED command in linux is, how it works, syntax, and options of the SED command and practical examples of its uses.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-26T03:30:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-26T05:04:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sed-command-in-linux.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sed Command in Linux - DataFlair","description":"Learn what SED command in linux is, how it works, syntax, and options of the SED command and practical examples of its uses.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"Sed Command in Linux - DataFlair","og_description":"Learn what SED command in linux is, how it works, syntax, and options of the SED command and practical examples of its uses.","og_url":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2022-07-26T03:30:55+00:00","article_modified_time":"2022-07-26T05:04:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sed-command-in-linux.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Sed Command in Linux","datePublished":"2022-07-26T03:30:55+00:00","dateModified":"2022-07-26T05:04:02+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/"},"wordCount":1883,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sed-command-in-linux.webp","keywords":["Sed Command in Linux"],"articleSection":["Linux Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/","url":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/","name":"Sed Command in Linux - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sed-command-in-linux.webp","datePublished":"2022-07-26T03:30:55+00:00","dateModified":"2022-07-26T05:04:02+00:00","description":"Learn what SED command in linux is, how it works, syntax, and options of the SED command and practical examples of its uses.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sed-command-in-linux.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sed-command-in-linux.webp","width":1200,"height":628,"caption":"sed command in linux"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/sed-command-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Linux Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/linux\/"},{"@type":"ListItem","position":3,"name":"Sed Command in Linux"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=110397"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110397\/revisions"}],"predecessor-version":[{"id":110493,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110397\/revisions\/110493"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/110461"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=110397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=110397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=110397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}