{"id":589788,"date":"2023-09-03T19:00:27","date_gmt":"2023-09-03T16:00:27","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-the-sed-command-on-linux\/"},"modified":"2023-09-03T19:00:27","modified_gmt":"2023-09-03T16:00:27","slug":"how-to-use-the-sed-command-on-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/","title":{"rendered":"#How to Use the sed Command on Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a26eaa4320f2\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a26eaa4320f2\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Quick_Links\" >Quick Links<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Key_Takeaways\" >Key Takeaways<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#The_Power_of_sed\" >The Power of sed<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#A_Simple_Example\" >A Simple Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Selecting_Text\" >Selecting Text<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Making_Substitutions\" >Making Substitutions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#More_Complex_Substitutions\" >More Complex Substitutions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Inserting_Lines_and_Text\" >Inserting Lines and Text<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Deleting_Lines\" >Deleting Lines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Saving_Your_Changes\" >Saving Your Changes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-sed-command-on-linux\/#Having_sed_All_That\" >Having sed All That<\/a><\/li><\/ul><\/nav><\/div>\n<div>\n<div class=\"article-table-contents\">\n<h3><span class=\"ez-toc-section\" id=\"Quick_Links\"><\/span>Quick Links<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul class=\"table-content-level-1\">\n<li> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"#more-complex-substitutions\">More Complex Substitutions<\/a> <\/li>\n<\/ul>\n<\/div>\n<section class=\"emaki-custom-block emaki-custom-key-points\">\n<div class=\"emaki-custom key-points\" id=\"custom_block_0\">\n<h3 class=\"title icon i-list\"><span class=\"ez-toc-section\" id=\"Key_Takeaways\"><\/span>Key Takeaways<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"custom_block-content\">\n<ul>\n<li> The Linux sed command is a powerful text editor without an interface, used for manipulating text in files and streams. <\/li>\n<li> Sed can select, substitute, add, delete, and modify text in files and streams, providing instructions for it to follow as it works. <\/li>\n<li> Sed relies heavily on regular expressions for pattern matching and text selection, and familiarity with regexes is important for using sed effectively. <\/li>\n<\/ul>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":0,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":0,\"nbrPlacementsScanned\":0,\"ruleCount\":10,\"degradationStartingPoint\":2,\"actualCount\":241} --><\/section>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":0,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":0,\"nbrPlacementsScanned\":1,\"ruleCount\":10,\"degradationStartingPoint\":2,\"actualCount\":0} --><!-- Zone: below first paragraph. --><br \/>\nIt might sound crazy, but the Linux <code>sed<\/code> command is a text editor without an interface. You can use it from the command line to manipulate text in files and streams. We&#8217;ll show you how to harness its power.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":0,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":0,\"nbrPlacementsScanned\":1,\"ruleCount\":10,\"degradationStartingPoint\":2,\"actualCount\":209} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":1,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":10,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\n<!-- No winning ad found for zone: native in content! --><\/p>\n<h2 id=\"the-power-of-sed\"><span class=\"ez-toc-section\" id=\"The_Power_of_sed\"><\/span> The Power of sed <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>sed<\/code> command is a bit like chess: it takes an hour to learn the basics and a lifetime to master them (or, at least a lot of practice). We&#8217;ll show you a selection of opening gambits in each of the main categories of <code>sed<\/code> functionality.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":240} --><br \/>\n<code>sed<\/code> is a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/sed.1.html\">stream editor<\/a> that works on piped input or files of text. It doesn&#8217;t have an interactive text editor interface, however. Rather, you provide instructions for it to follow as it works through the text. This all works in Bash and other command-line shells.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":507} --><br \/>\nWith <code>sed<\/code> you can do all of the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":548} --><\/p>\n<ul>\n<li> Select text <\/li>\n<li> Substitute text <\/li>\n<li> Add lines to text <\/li>\n<li> Delete lines from text <\/li>\n<li> Modify (or preserve) an original file <\/li>\n<\/ul>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":666} -->We&#8217;ve structured our examples to introduce and demonstrate concepts, not to produce the tersest (and least <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roachable) <code>sed<\/code> commands. However, the pattern matching and text selection functionalities of <code>sed<\/code> rely heavily on regular expressions (regexes). You&#8217;re going to need some familiarity with these to get the best out of <code>sed<\/code>.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":1,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1004} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<h2 id=\"a-simple-example\"><span class=\"ez-toc-section\" id=\"A_Simple_Example\"><\/span> A Simple Example <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First, we&#8217;re going to use <code>echo<\/code> to send some text to <code>sed<\/code> through a pipe, and have <code>sed<\/code> substitute a portion of the text. To do so, we type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":155} --><\/p>\n<pre>echo howtogonk | sed 's\/gonk\/geek\/'<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":198} --><br \/>\nThe <code>echo<\/code> command sends &#8220;howtogonk&#8221; into <code>sed<\/code>, and our simple substitution rule (the &#8220;s&#8221; stands for substitution) is applied. <code>sed<\/code> searches the input text for an occurrence of the first string, and will replace any matches with the second.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":454} --><br \/>\nThe string &#8220;gonk&#8221; is replaced by &#8220;geek,&#8221; and the new string is printed in the terminal window.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":568} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:15.015479876161%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/34.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/34.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/34.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/34.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/34.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"97\" alt=\"echo howtogonk | sed 's\/gonk\/geek\/' in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/34.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":808} --><br \/>\nSubstitutions are probably the most common use of <code>sed<\/code>. Before we can dive deeper into substitutions, though, we need to know how to select and match text.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":2,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":962} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<h2 id=\"selecting-text\"><span class=\"ez-toc-section\" id=\"Selecting_Text\"><\/span> Selecting Text <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We&#8217;re going to need a text file for our examples. We&#8217;ll use one that contains a selection of verses from Samuel Taylor Coleridge&#8217;s epic poem &#8220;The Rime of the Ancient Mariner.&#8221;<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":197} --><br \/>\nWe type the following to take a look at it with <code>less<\/code>:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":250} --><\/p>\n<pre>less coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":268} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/2-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/2-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/2-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/2-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/2-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"less coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/2-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":508} --><br \/>\nTo select some lines from the file, we provide the start and end lines of the range we want to select. A single number selects that one line.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":649} --><br \/>\nTo extract lines one to four, we type this command:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":700} --><\/p>\n<pre>sed -n '1,4p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":735} --><br \/>\nNote the comma between <code>1<\/code> and <code>4<\/code>. The <code>p<\/code> means &#8220;print matched lines.&#8221; By default, <code>sed<\/code> prints all lines. We&#8217;d see all the text in the file with the matching lines printed twice. To prevent this, we&#8217;ll use the <code>-n<\/code> (quiet) option to suppress the unmatched text.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":3,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1007} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe change the line numbers so we can select a different verse, as shown below:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":78} --><\/p>\n<pre>sed -n '6,9p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":113} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:38.235294117647%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/3-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/3-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/3-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/3-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/3-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"247\" alt=\"sed -n '1,4p' coleridge.txt ina terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/3-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":353} --><br \/>\nWe can use the <code>-e<\/code> (expression) option to make multiple selections. With two expressions, we can select two verses, like so:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":476} --><\/p>\n<pre>sed -n -e '1,4p' -e '31,34p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":534} --><br \/>\nIf we reduce the first number in the second expression, we can insert a blank between the two verses. We type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":658} --><\/p>\n<pre>sed -n -e '1,4p' -e '30,34p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":716} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/4-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/4-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/4-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/4-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/4-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"sed -n -e '1,4p' -e '31,34p' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/4-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":4,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":956} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe can also choose a starting line and tell <code>sed<\/code> to step through the file and print alternate lines, every fifth line, or to skip any number of lines. The command is similar to those we used above to select a range. This time, however, we&#8217;ll use a tilde (<code>~<\/code>) instead of a comma to separate the numbers.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":304} --><br \/>\nThe first number indicates the starting line. The second number tells <code>sed<\/code> which lines after the starting line we want to see. The number 2 means every second line, 3 means every third line, and so on.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":504} --><br \/>\nWe type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":526} --><\/p>\n<pre>sed -n '1~2p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":561} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/6-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/6-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/6-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/6-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/6-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"sed -n '1~2p' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/6-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":801} --><br \/>\nYou won&#8217;t always know where the text you&#8217;re looking for is located in the file, which means line numbers won&#8217;t always be much help. However, you can also use <code>sed<\/code> to select lines that contain matching text patterns. For example, let&#8217;s extract all lines that start with &#8220;And.&#8221;<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":5,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1101} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nThe caret (<code>^<\/code>) represents the start of the line. We&#8217;ll enclose our search term in forward slashes (<code>\/<\/code>). We also include a space after &#8220;And&#8221; so words like &#8220;Android&#8221; won&#8217;t be included in the result.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":222} --><br \/>\nReading <code>sed<\/code> scripts can be a bit tough at first. The <code>\/p<\/code> means &#8220;print,&#8221; just as it did in the commands we used above. In the following command, though, a forward slash precedes it:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":411} --><\/p>\n<pre>sed -n '\/^And \/p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":450} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:20.433436532508%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/5-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/5-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/5-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/5-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/5-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"132\" alt=\"sed -n '\/^And \/p' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/5-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":690} --><br \/>\nThree lines that start with &#8220;And &#8221; are extracted from the file and displayed for us.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":784} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":784} --><\/p>\n<h2 id=\"making-substitutions\"><span class=\"ez-toc-section\" id=\"Making_Substitutions\"><\/span> Making Substitutions <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In our first example, we showed you the following basic format for a <code>sed<\/code> substitution:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":870} --><\/p>\n<pre>echo howtogonk | sed 's\/gonk\/geek\/'<\/pre>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":6,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":913} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nThe <code>s<\/code> tells <code>sed<\/code> this is a substitution. The first string is the search pattern, and the second is the text with which we want to replace that matched text. Of course, as with all things Linux, the devil is in the details.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":221} --><br \/>\nWe type the following to change all occurrences of &#8220;day&#8221; to &#8220;week,&#8221; and give the mariner and albatross more time to bond:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":362} --><\/p>\n<pre>sed -n 's\/day\/week\/p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":405} --><br \/>\nIn the first line, only the second occurrence of &#8220;day&#8221; is changed. This is because <code>sed<\/code> stops after the first match per line. We have to add a &#8220;g&#8221; at the end of the expression, as shown below, to perform a global search so all matches in each line are processed:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":686} --><\/p>\n<pre>sed -n 's\/day\/week\/gp' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":730} --><br \/>\nThis matches three out of the four in the first line. Because the first word is &#8220;Day,&#8221; and <code>sed<\/code> is case-sensitive, it doesn&#8217;t consider that instance to be the same as &#8220;day.&#8221;<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":7,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":926} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe type the following, adding an <code>i<\/code> to the command at the end of the expression to indicate case-insensitivity:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":110} --><\/p>\n<pre>sed -n 's\/day\/week\/gip' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":155} --><br \/>\nThis works, but you might not always want to turn on case-insensitivity for everything. In those instances, you can use a regex group to add pattern-specific case-insensitivity.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":332} --><br \/>\nFor example, if we enclose characters in square brackets (<code>[]<\/code>), they&#8217;re interpreted as &#8220;any character from this list of characters.&#8221;<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":477} --><br \/>\nWe type the following, and include &#8220;D&#8221; and &#8220;d&#8221; in the group, to ensure it matches both &#8220;Day&#8221; and &#8220;day&#8221;:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":620} --><\/p>\n<pre>sed -n 's\/[Dd]ay\/week\/gp' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":667} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:52.167182662539%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/16.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/16.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/16.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/16.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/16.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"337\" alt=\"sed -n 's\/day\/week\/p' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/16.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":8,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":907} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe can also restrict substitutions to sections of the file. Let&#8217;s say our file contains weird spacing in the first verse. We can use the following familiar command to see the first verse:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":191} --><\/p>\n<pre>sed -n '1,4p' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":226} --><br \/>\nWe&#8217;ll search for two spaces and substitute them with one. We&#8217;ll do this globally so the action is repeated across the entire line. To be clear, the search pattern is space, space asterisk (<code>*<\/code>), and the substitution string is a single space. The <code>1,4<\/code> restricts the substitution to the first four lines of the file.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":545} --><br \/>\nWe put all of that together in the following command:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":598} --><\/p>\n<pre>sed -n '1,4 s\/ *\/ \/gp' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":642} --><br \/>\nThis works nicely! The search pattern is what&#8217;s important here. The asterisk (<code>*<\/code>) represents zero or more of the preceding character, which is a space. Thus, the search pattern is looking for strings of one space or more.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":866} --><br \/>\nIf we substitute a single space for any sequence of multiple spaces, we&#8217;ll return the file to regular spacing, with a single space between each word. This will also substitute a single space for a single space in some cases, but this won&#8217;t affect anything adversely&#8212;we&#8217;ll still get our desired result.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":9,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1181} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nIf we type the following and reduce the search pattern to a single space, you&#8217;ll see im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely why we have to include two spaces:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":135} --><\/p>\n<pre>sed -n '1,4 s\/ *\/ \/gp' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":179} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/17.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/17.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/17.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/17.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/17.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"sed -n '1,4p' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/17.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":419} --><br \/>\nBecause the asterisk matches zero or more of the preceding character, it sees each character that isn&#8217;t a space as a &#8220;zero space&#8221; and applies the substitution to it.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":598} --><br \/>\nHowever, if we include two spaces in the search pattern, <code>sed<\/code> must find at least one space character before it applies the substitution. This ensures nonspace characters will remain untouched.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":789} --><br \/>\nWe type the following, using the <code>-e<\/code> (expression) we used earlier, which allows us to make two or more substitutions simultaneously:<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":10,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":920} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<pre>sed -n -e 's\/motion\/flutter\/gip' -e 's\/ocean\/gutter\/gip' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":86} --><br \/>\nWe can achieve the same result if we use a semicolon (<code>;<\/code>) to separate the two expressions, like so:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":184} --><\/p>\n<pre>sed -n 's\/motion\/flutter\/gip;s\/ocean\/gutter\/gip' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":254} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:35.139318885449%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/18.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/18.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/18.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/18.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/18.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"227\" alt=\"sed -n -e 's\/motion\/flutter\/gip' -e 's\/ocean\/gutter\/gip' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/18.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":494} --><br \/>\nWhen we swapped &#8220;day&#8221; for &#8220;week&#8221; in the following command, the instance of &#8220;day&#8221; in the expression &#8220;well a-day&#8221; was swapped as well:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":666} --><\/p>\n<pre>sed -n 's\/[Dd]ay\/week\/gp' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":713} --><br \/>\nTo prevent this, we can only attempt substitutions on lines that match another pattern. If we modify the command to have a search pattern at the start, we&#8217;ll only consider operating on lines that match that pattern.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":11,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":932} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe type the following to make our matching pattern the word &#8220;after&#8221;:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":78} --><\/p>\n<pre>sed -n '\/after\/ s\/[Dd]ay\/week\/gp' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":133} --><br \/>\nThat gives us the response we want.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":168} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:25.851393188854%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/20.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/20.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/20.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/20.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/20.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"167\" alt=\"sed -n 's\/[Dd]ay\/week\/gp' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/20.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":408} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":408} --><\/p>\n<h2 id=\"more-complex-substitutions\"><span class=\"ez-toc-section\" id=\"More_Complex_Substitutions\"><\/span> More Complex Substitutions <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let&#8217;s give Coleridge a break and use <code>sed<\/code> to extract names from the <code>etc\/passwd<\/code> file.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":495} --><br \/>\nThere are shorter ways to do this (more on that later), but we&#8217;ll use the longer way here to demonstrate another concept. Each matched item in a search pattern (called subexpressions) can be numbered (up to a maximum of nine items). You can then use these numbers in your <code>sed<\/code> commands to reference specific subexpressions.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":821} --><br \/>\nYou have to enclose the subexpression in parentheses [<code>()<\/code>] for this to work. The parentheses also must be preceded by a backward slash (<code>\\<\/code>) to prevent them from being treated as a normal character.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":12,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1016} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":13,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nTo do this, you would type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":13,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":41} --><\/p>\n<pre>sed 's\/\\([^:]*\\).*\/\\1\/' \/etc\/passwd<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":13,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":84} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:8.8235294117647%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/12-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/12-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/12-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/12-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/12-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"57\" alt=\"sed 's\/\\([^:]*\\).*\/\\1\/' \/etc\/passwd in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/12-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":13,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":324} --><br \/>\nLet&#8217;s break this down:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":13,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":350} --><\/p>\n<ul>\n<li> <strong><code>sed 's\/<\/code>:<\/strong> The <code>sed<\/code> command and the beginning of the substitution expression. <\/li>\n<li> <strong><code>\\(<\/code>:<\/strong> The opening parenthesis [<code>(<\/code>] enclosing the subexpression, preceded by a backslash (<code>\\<\/code>). <\/li>\n<li> <strong><code>[^:]*<\/code>:<\/strong> The first subexpression of the search term contains a group in square brackets. The caret (<code>^<\/code>) means &#8220;not&#8221; when used in a group. A group means any character that isn&#8217;t a colon (<code>:<\/code>) will be accepted as a match. <\/li>\n<li> <strong><code>\\)<\/code>:<\/strong> The closing parenthesis [<code>)<\/code>] with a preceding backslash (<code>\\<\/code>). <\/li>\n<li> <strong><code>.*<\/code>:<\/strong> This second search subexpression means &#8220;any character and any number of them.&#8221; <\/li>\n<li> <strong><code>\/\\1<\/code>:<\/strong> The substitution portion of the expression contains <code>1<\/code> preceded by a backslash (<code>\\<\/code>). This represents the text that matches the first subexpression. <\/li>\n<li> <strong><code>\/'<\/code>:<\/strong> The closing forward-slash (<code>\/<\/code>) and single quote (<code>'<\/code>) terminate the <code>sed<\/code> command. <\/li>\n<\/ul>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":13,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1161} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":14,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWhat this all means is we&#8217;re going to look for any string of characters that doesn&#8217;t contain a colon (<code>:<\/code>), which will be the first instance of matching text. Then, we&#8217;re searching for anything else on that line, which will be the second instance of matching text. We&#8217;re going to substitute the entire line with the text that matched the first subexpression.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":14,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":15,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":372} --><br \/>\nEach line in the <code>\/etc\/passwd<\/code> file starts with a colon-terminated username. We match everything up to the first colon, and then substitute that value for the entire line. So, we&#8217;ve isolated the usernames.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":14,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":15,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":579} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/13-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/13-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/13-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/13-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/13-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"Output from\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/13-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":14,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":15,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":819} --><br \/>\nNext, we&#8217;ll enclose the second subexpression in parentheses [<code>()<\/code>] so we can reference it by number, as well. We&#8217;ll also replace <code>\\1<\/code> with <code>\\2<\/code>. Our command will now substitute the entire line with everything from the first colon (<code>:<\/code>) to the end of the line.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":14,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":15,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1078} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":15,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":22} --><\/p>\n<pre>sed 's\/\\([^:]*\\)\\(.*\\)\/\\2\/' \/etc\/passwd<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":69} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:8.8235294117647%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/14-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/14-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/14-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/14-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/14-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"57\" alt=\"sed 's\/\\([^:]*\\)\\(.*\\)\/\\2\/' \/etc\/passwd in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/14-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":309} --><br \/>\nThose small changes invert the meaning of the command, and we get everything except the usernames.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":407} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/15.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/15.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/15.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/15.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/15.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"Output from sed 's\/\\([^:]*\\)\\(.*\\)\/\\2\/' \/etc\/passwd in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/15.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":647} --><br \/>\nNow, let&#8217;s take a look at the quick and easy way to do this.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":711} --><br \/>\nOur search term is from the first colon (<code>:<\/code>) to the end of the line. Because our substitution expression is empty (<code>\/\/<\/code>), we won&#8217;t replace the matched text with anything.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":882} --><br \/>\nSo, we type the following, chopping off everything from the first colon (<code>:<\/code>) to the end of the line, leaving just the usernames:<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":15,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":15,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1009} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":16,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<pre>sed 's\/:.*\/\/\" \/etc\/passwd<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":34} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:8.8235294117647%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/10-1.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/10-1.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/10-1.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/10-1.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/10-1.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"57\" alt=\"sed 's\/:.*\/\/\" in=\"\" a=\"\" terminal=\"\" window=\"\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/10-1.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":274} --><br \/>\nLet&#8217;s look at an example in which we reference the first and second matches in the same command.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":374} --><br \/>\nWe&#8217;ve got a file of commas (<code>,<\/code>) separating first and last names. We want to list them as &#8220;last name, first name.&#8221; We can use <code>cat<\/code>, as shown below, to see what&#8217;s in the file:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":563} --><\/p>\n<pre>cat geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":576} --><br \/>\nLike a lot of <code>sed<\/code> commands, this next one might look impenetrable at first:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":651} --><\/p>\n<pre>sed 's\/^\\(.*\\),\\(.*\\)$\/\\2,\\1 \/g' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":701} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/19.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/19.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/19.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/19.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/19.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"sed 's\/^\\(.*\\),\\(.*\\)$\/\\2,\\1 \/g' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/19.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":16,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":16,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":941} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":17,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":17,\"nbrPlacementsScanned\":17,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nThis is a substitution command like the others we&#8217;ve used, and the search pattern is quite easy. We&#8217;ll break it down below:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":17,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":17,\"nbrPlacementsScanned\":18,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":131} --><\/p>\n<ul>\n<li> <strong><code>sed 's\/<\/code>:<\/strong> The normal substitution command. <\/li>\n<li> <strong><code>^<\/code>:<\/strong> Because the caret isn&#8217;t in a group (<code>[]<\/code>), it means &#8220;The start of the line.&#8221; <\/li>\n<li> <strong><code>\\(.*\\),<\/code>:<\/strong> The first subexpression is any number of any characters. It&#8217;s enclosed in parentheses [<code>()<\/code>], each of which is preceded by a backslash (<code>\\<\/code>) so we can reference it by number. Our entire search pattern so far translates as search from the start of the line up to the first comma (<code>,<\/code>) for any number of any characters. <\/li>\n<li> <strong><code>\\(.*\\)<\/code>:<\/strong> The next subexpression is (again) any number of any character. It&#8217;s also enclosed in parentheses [<code>()<\/code>], both of which are preceded by a backslash (<code>\\<\/code>) so we can reference the matching text by number. <\/li>\n<li> <strong><code>$\/<\/code>:<\/strong> The dollar sign (<code>$<\/code>) represents the end of the line and will allow our search to continue to the end of the line. We&#8217;ve used this simply to introduce the dollar sign. We don&#8217;t really need it here, as the asterisk (<code>*<\/code>) would go to the end of the line in this scenario. The forward slash (<code>\/<\/code>) completes the search pattern section. <\/li>\n<li> <strong><code>\\2,\\1 \/g'<\/code>:<\/strong> Because we enclosed our two subexpressions in parentheses, we can refer to both of them by their numbers. Because we want to reverse the order, we type them as <code>second-match,first-match<\/code>. The numbers have to be preceded by a backslash (<code>\\<\/code>). <\/li>\n<li> <strong><code>\/g<\/code>:<\/strong> This enables our command to work globally on each line. <\/li>\n<li> <strong><code>geeks.txt<\/code>:<\/strong> The file we&#8217;re working on. <\/li>\n<\/ul>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":17,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":17,\"nbrPlacementsScanned\":18,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1514} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":18,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nYou can also use the Cut command (<code>c<\/code>) to substitute entire lines that match your search pattern. We type the following to search for a line with the word &#8220;neck&#8221; in it, and replace it with a new string of text:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":218} --><\/p>\n<pre>sed '\/neck\/c Around my wrist was strung' coleridge.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":280} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:11.919504643963%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/35.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/35.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/35.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/35.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/35.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"77\" alt=\"sed '\/neck\/c Around my wrist was strung' coleridge.txt\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/35.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":520} --><br \/>\nOur new line now appears at the bottom of our extract.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":574} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:59.133126934985%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/36.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/36.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/36.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/36.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/36.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"382\" alt=\"Output from sed '\/neck\/c Around my wrist was strung' coleridge.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/36.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":814} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":814} --><\/p>\n<h2 id=\"inserting-lines-and-text\"><span class=\"ez-toc-section\" id=\"Inserting_Lines_and_Text\"><\/span> Inserting Lines and Text <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can also insert new lines and text into our file. To insert new lines after any matching ones, we&#8217;ll use the Append command (<code>a<\/code>).<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":18,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":18,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":949} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":19,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nHere&#8217;s the file we&#8217;re going to work with:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":49} --><br \/>\ncat geeks.txt<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":62} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:32.8173374613%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/22.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/22.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/22.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/22.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/22.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"212\" alt=\"cat geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/22.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":302} --><br \/>\nWe&#8217;ve numbered the lines to make this a bit easier to follow.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":367} --><br \/>\nWe type the following to search for lines that contain the word &#8220;He,&#8221; and insert a new line beneath them:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":482} --><\/p>\n<pre>sed '\/He\/a --&gt; Inserted!' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":528} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:40.557275541796%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/23.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/23.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/23.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/23.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/23.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img width=\"646\" class=\"lazyload\" height=\"262\" alt=\"sed '\/He\/a --&gt; Inserted!' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/23.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":768} --><br \/>\nWe type the following and include the Insert Command (<code>i<\/code>) to insert the new line above those that contain matching text:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":887} --><\/p>\n<pre>sed '\/He\/i --&gt; Inserted!' geeks.txt<\/pre>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":19,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":19,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":933} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":20,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:40.557275541796%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/24.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/24.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/24.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/24.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/24.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img width=\"646\" class=\"lazyload\" height=\"262\" alt=\"sed '\/He\/i --&gt; Inserted!' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/24.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":240} --><br \/>\nWe can use the ampersand (<code>&amp;<\/code>), which represents the original matched text, to add new text to a matching line. <code>\\1<\/code> , <code>\\2<\/code>, and so on, represent matching subexpressions.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":408} --><br \/>\nTo add text to the start of a line, we&#8217;ll use a substitution command that matches everything on the line, combined with a replacement clause that combines our new text with the original line.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":603} --><br \/>\nTo do all of this, we type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":644} --><\/p>\n<pre>sed 's\/.*\/--&gt; Inserted &amp;\/' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":695} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:32.8173374613%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/25.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/25.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/25.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/25.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/25.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img width=\"646\" class=\"lazyload\" height=\"212\" alt=\"sed 's\/.*\/--&gt; Inserted &amp;\/' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/25.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":20,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":20,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":935} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":21,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nWe type the following, including the <code>G<\/code> command, which will add a blank line between each line:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":94} --><\/p>\n<pre>sed 'G' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":119} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:52.167182662539%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/26.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/26.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/26.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/26.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/26.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"337\" alt=\"sed 'G' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/26.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":359} --><br \/>\nIf you want to add two or more blank lines, you can use <code>G;G<\/code>, <code>G;G;G<\/code>, and so on.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":437} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":437} --><\/p>\n<h2 id=\"deleting-lines\"><span class=\"ez-toc-section\" id=\"Deleting_Lines\"><\/span> Deleting Lines <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Delete command (<code>d<\/code>) deletes lines that match a search pattern, or those specified with line numbers or ranges.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":550} --><br \/>\nFor example, to delete the third line, we would type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":617} --><\/p>\n<pre>sed '3d' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":643} --><br \/>\nTo delete the range of lines four to five, we&#8217;d type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":714} --><\/p>\n<pre>sed '4,5d' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":742} --><br \/>\nTo delete lines outside a range, we use an exclamation point (<code>!<\/code>), as shown below:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":823} --><\/p>\n<pre>sed '6,7!d' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":852} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:54.489164086687%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/27.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/27.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/27.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/27.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/27.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"352\" alt=\"sed '3d' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/27.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":21,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":21,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":1092} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":22,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<h2 id=\"saving-your-changes\"><span class=\"ez-toc-section\" id=\"Saving_Your_Changes\"><\/span> Saving Your Changes <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>So far, all of our results have printed to the terminal window, but we haven&#8217;t yet saved them anywhere. To make these permanent, you can either write your changes to the original file or redirect them to a new one.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":218} --><br \/>\nOverwriting your original file requires some caution. If your <code>sed<\/code> command is wrong, you might make some changes to the original file that are difficult to undo.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":378} --><br \/>\nFor some peace of mind, <code>sed<\/code> can create a backup of the original file before it executes its command.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":478} --><br \/>\nYou can use the In-place option (<code>-i<\/code>) to tell <code>sed<\/code> to write the changes to the original file, but if you add a file extension to it, <code>sed<\/code> will back up the original file to a new one. It will have the same name as the original file, but with a new file extension.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":737} --><br \/>\nTo demonstrate, we&#8217;ll search for any lines that contain the word &#8220;He&#8221; and delete them. We&#8217;ll also back up our original file to a new one using the BAK extension.<br \/>\n<!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":22,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":22,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":916} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":23,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><br \/>\nTo do all of this, we type the following:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":41} --><\/p>\n<pre>sed -i'.bak' '\/^.*He.*$\/d' geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":93} --><br \/>\nWe type the following to make sure our backup file is unchanged:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":157} --><\/p>\n<pre>cat geeks.txt.bak<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":174} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:49.84520123839%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/28.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/28.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/28.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/28.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/28.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img loading=\"lazy\" decoding=\"async\" width=\"646\" class=\"lazyload\" height=\"322\" alt=\"sed -i'.bak' '\/^.*He.*\/d' geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" src=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/28.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":414} --><br \/>\nWe can also type the following to redirect the output to a new file and achieve a similar result:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":511} --><\/p>\n<pre>sed -i'.bak' '\/^.*He.*$\/d' geeks.txt &gt; new_geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":582} --><br \/>\nWe use <code>cat<\/code> to confirm the changes were written to the new file, as shown below:<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":661} --><\/p>\n<pre>cat new_geeks.txt<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":678} --><\/p>\n<div class=\"body-img landscape\">\n<div class=\"responsive-img image-expandable img-article-item\" style=\"padding-bottom:47.523219814241%\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/29.png\" data-modal-id=\"single-image-modal\" data-modal-container-id=\"single-image-modal-container\" data-img-caption=\"null\">\n<figure><picture><!--[if IE 9]> <video style=\"display: none;\"><![endif]--><source media=\"(min-width: 1024px)\" sizes=\"750px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/29.png?q=50&amp;fit=crop&amp;w=750&amp;dpr=1.5\"\/><source media=\"(min-width: 768px)\" sizes=\"943px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/29.png?q=50&amp;fit=crop&amp;w=943&amp;dpr=1.5\"\/><source media=\"(min-width: 481px)\" sizes=\"767px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/29.png?q=50&amp;fit=crop&amp;w=767&amp;dpr=1.5\"\/><source media=\"(min-width: 0px)\" sizes=\"480px\" data-srcset=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/29.png?q=50&amp;fit=crop&amp;w=480&amp;dpr=1.5\"\/><!--[if IE 9]><\/video><![endif]--><img width=\"646\" class=\"lazyload\" height=\"307\" alt=\"sed -i'.bak' '\/^.*He.*$\/d' geeks.txt &gt; new_geeks.txt in a terminal window\" style=\"display:block;height:auto;max-width:100%;\" data-img-url=\"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2020\/04\/29.png\"\/><\/picture><\/figure>\n<\/div><\/div>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":23,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":23,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":918} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":24,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":24,\"nbrPlacementsScanned\":24,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":24,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":24,\"nbrPlacementsScanned\":25,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":0} --><\/p>\n<h2 id=\"having-sed-all-that\"><span class=\"ez-toc-section\" id=\"Having_sed_All_That\"><\/span> Having sed All That <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As you&#8217;ve probably noticed, even this quick primer on <code>sed<\/code> is quite long. There&#8217;s a lot to this command, and there&#8217;s even <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.gnu.org\/software\/sed\/manual\/sed.html\">more you can do with it<\/a>.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":24,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":24,\"nbrPlacementsScanned\":25,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":157} --><br \/>\nHopefully, though, these basic concepts have provided a solid foundation on which you can build as you continue to learn more.<br \/>\n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":24,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":24,\"nbrPlacementsScanned\":25,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":283} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":24,\"startingPoint\":1,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":24,\"nbrPlacementsScanned\":25,\"ruleCount\":900,\"degradationStartingPoint\":2,\"actualCount\":603} --><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/666395\/how-to-use-the-sed-command-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick Links More Complex Substitutions Key Takeaways The Linux sed command is a powerful text editor without an interface, used for manipulating text in files and streams. Sed can select, substitute, add, delete, and modify text in files and streams, providing instructions for it to follow as it works. Sed relies heavily on regular expressions&#8230;<\/p>\n","protected":false},"author":1,"featured_media":589789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/static1.howtogeekimages.com\/wordpress\/wp-content\/uploads\/2023\/09\/52848911559_e76b589879_o.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-589788","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/589788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=589788"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/589788\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/589789"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=589788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=589788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=589788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}