{"id":5342,"date":"2019-06-04T21:31:25","date_gmt":"2019-06-05T02:31:25","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=5342"},"modified":"2019-06-14T19:09:21","modified_gmt":"2019-06-15T00:09:21","slug":"using-sed-to-insert-lines-into-a-file","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=5342","title":{"rendered":"Using sed to insert lines into a file"},"content":{"rendered":"<p>I&#8217;ve used sed to replace file content &#8212; use a regex to replace the sendmail.cf line that routes mail directly with a smarthost directive<\/p>\n<pre>sed -i -e 's\/^DS\/DS\\\\\\[mailTWB.example.com\\\\\\]\/' $strSendmailDirectory\/etc\/mail\/sendmail.cf<\/pre>\n<p>But I&#8217;ve needed to prepend text to a file. Turns out sed acn do that. In fact, you can insert strings at any line number. Using &#8220;sed -i &#8216;5s;^;StringsToInsert\\n;&#8217; filename.xtn will insert &#8220;StringsToInsert\\n&#8221; at line 5. To prepend text to a file, use &#8220;1s&#8221;<\/p>\n<p>[lisa@fedora tmp]# cat test.txt;sed -i &#8216;5s;^;NewLine1\\nNewLine2\\n;&#8217; test.txt;cat test.txt<br \/>\nLine 1<br \/>\nLine 2<br \/>\nLine 3<br \/>\nLine 4<br \/>\nLine 5<br \/>\nLine 6<br \/>\n**********<br \/>\nLine 1<br \/>\nLine 2<br \/>\nLine 3<br \/>\nLine 4<br \/>\nNewLine1<br \/>\nNewLine2<br \/>\nLine 5<br \/>\nLine 6<br \/>\n**********<\/p>\n<p>&nbsp;<\/p>\n<p>I&#8217;ve also come across an oddity in the Win32 sed &#8212; the method I usually use to blow away everything <em>after<\/em> a newline for some reason blows away everything after the <em>first<\/em> line. Works fine on RHEL7 and Fedora29, so the quick solution is &#8220;run it from the Linux box&#8221;.<\/p>\n<p>C:\\temp&gt;cat input.txt<br \/>\nline 1<br \/>\nline 2<\/p>\n<p>line 3<br \/>\nline 4<br \/>\nline 5<br \/>\nC:\\temp&gt;sed -i &#8216;\/^$\/q&#8217; input.txt&amp;cat input.txt<br \/>\nline 1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve used sed to replace file content &#8212; use a regex to replace the sendmail.cf line that routes mail directly with a smarthost directive sed -i -e &#8216;s\/^DS\/DS\\\\\\[mailTWB.example.com\\\\\\]\/&#8217; $strSendmailDirectory\/etc\/mail\/sendmail.cf But I&#8217;ve needed to prepend text to a file. Turns out sed acn do that. In fact, you can insert strings at any line number. Using &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,30],"tags":[632,294,780],"class_list":["post-5342","post","type-post","status-publish","format-standard","hentry","category-coding","category-system-administration","tag-bash","tag-linux","tag-sed"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5342"}],"version-history":[{"count":3,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5342\/revisions"}],"predecessor-version":[{"id":5345,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5342\/revisions\/5345"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}