{"id":9451,"date":"2025-02-03T03:00:00","date_gmt":"2025-02-03T08:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=9451"},"modified":"2025-01-30T10:56:23","modified_gmt":"2025-01-30T15:56:23","slug":"quoting-text-with-sed","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=9451","title":{"rendered":"Quoting text with \u2018sed\u2019"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"9451\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p><strong>sed<\/strong> is a very useful tool that makes it easy to <a href=\"https:\/\/www.both.org\/?p=9447\">automate text file edits<\/a> from the command line. You can use other features of regular expressions to match specific instances of text, such as to \u201cquote\u201d text files to include them in the body of an email.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"matching-a-line\">Matching a line<\/h2>\n\n\n\n<p>For example, you might need to replace text that occurs at the start of a line. With <strong>sed<\/strong>, you can match the beginning of a line with <code>^<\/code>, the caret character. One way I use \u201cstart of line\u201d in replacing text is when I need to quote a file in an email.<\/p>\n\n\n\n<p>Let\u2019s say I want to share my Makefile with someone via email, but I don\u2019t want to include it as a file attachment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.PHONY: all run clean\n\nall: life\n\nlife: life.o\n       $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)\n\nrun: life\n       .\/life\n\nclean:\n       $(RM) *~\n       $(RM) *.o\n       $(RM) life<\/code><\/pre>\n\n\n\n<p>Instead, I prefer to \u201cquote\u201d the file in the body of an email, using <code>&gt;<\/code> before each line. I can use the following <strong>sed<\/strong> command to print out an edited version to my terminal, which I can copy and paste into a new email:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed -e 's\/^\/&gt;\/' Makefile\n&gt;.PHONY: all run clean\n&gt;\n&gt;all: life\n&gt;\n&gt;life: life.o\n&gt;       $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)\n&gt;\n&gt;run: life\n&gt;       .\/life\n&gt;\n&gt;clean:\n&gt;       $(RM) *~\n&gt;       $(RM) *.o\n&gt;       $(RM) life<\/code><\/pre>\n\n\n\n<p>The <code>s\/^\/&gt;\/<\/code> regular expression matches the start of each line (<code>^<\/code>) and places a <code>&gt;<\/code> there. Effectively, this starts each line with the <code>&gt;<\/code> symbol.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"replacing-tabs\">Replacing tabs<\/h2>\n\n\n\n<p>The tabs might not show up correctly in an email, but I can replace all tabs in the Makefile with a few spaces by adding another regular expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed -e 's\/^\/&gt;\/' -e 's\/\\t\/  \/g' Makefile\n&gt;.PHONY: all run clean\n&gt;\n&gt;all: life\n&gt;\n&gt;life: life.o\n&gt;  $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)\n&gt;\n&gt;run: life\n&gt;  .\/life\n&gt;\n&gt;clean:\n&gt;  $(RM) *~\n&gt;  $(RM) *.o\n&gt;  $(RM) life<\/code><\/pre>\n\n\n\n<p>The <code>\\t<\/code> indicates a literal tab, so <code>s\/\\t\/ \/g<\/code> tells <strong>sed<\/strong> to replace all tabs in the input with two spaces in the output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multiple-edits-at-once\">Multiple edits at once<\/h2>\n\n\n\n<p>If you need to apply lots of edits to files, you can save your <code>-e<\/code> commands in a file and use <code>-f<\/code> to tell <strong>sed<\/strong> to use that file as a \u201cscript.\u201d This approach is especially useful if you need to make the same edits frequently. I might have prepared the Makefile for quoting in email using a script file called <code>quotemail.sed<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s\/^\/&gt;\/\ns\/\\t\/  \/g<\/code><\/pre>\n\n\n\n<p>Then I can use the script like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed -f quotemail.sed Makefile\n&gt;.PHONY: all run clean\n&gt;\n&gt;all: life\n&gt;\n&gt;life: life.o\n&gt;  $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)\n&gt;\n&gt;run: life\n&gt;  .\/life\n&gt;\n&gt;clean:\n&gt;  $(RM) *~\n&gt;  $(RM) *.o\n&gt;  $(RM) life<\/code><\/pre>\n\n\n\n<p><strong>sed<\/strong> is a great tool to keep in your Linux command-line toolkit. Explore the <strong>sed<\/strong> manual page and learn more about how to use it. Type <code>man sed<\/code> at the command line to get complete documentation about the different command line options and how to use <strong>sed<\/strong> to process text files.<\/p>\n\n\n\n<p><em>This article is adapted from <a href=\"https:\/\/opensource.com\/article\/22\/8\/automate-file-edits-sed-linux\">How I use the Linux sed command to automate file edits<\/a> by Jim Hall, and is republished with the author\u2019s permission.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another great use of sed to make quick edits to a text file.<\/p>\n","protected":false},"author":33,"featured_media":2816,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,5],"tags":[104,91],"class_list":["post-9451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-linux","tag-command-line","tag-linux"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9451"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9451\/revisions"}],"predecessor-version":[{"id":9452,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9451\/revisions\/9452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2816"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}