{"id":9447,"date":"2025-01-31T02:00:00","date_gmt":"2025-01-31T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=9447"},"modified":"2025-01-30T10:40:50","modified_gmt":"2025-01-30T15:40:50","slug":"how-i-automate-file-edits-with-sed","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=9447","title":{"rendered":"How I automate file edits 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=\"9447\" 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>When I use the Linux command line, whether I\u2019m writing a new program on my desktop computer or managing a website on my web server, I often need to process text files. Linux provides powerful tools that I leverage to get my work done. I frequently use <strong>sed<\/strong>, an editor that can modify text according to a pattern.<\/p>\n\n\n\n<p><strong>sed<\/strong> stands for stream editor, and it edits text in a file and prints the results. One way to use <strong>sed<\/strong> is to identify several occurrences of one string in a file and replace them with a different string. You can use <strong>sed<\/strong> to process text files to a seemingly endless degree, but I\u2019d like to share one way I use <strong>sed<\/strong> to help me manage files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"search-and-replace-text\">Search and replace text<\/h2>\n\n\n\n<p>To use <strong>sed<\/strong>, you need to use a <em>regular expression<\/em>. A regular expression is a set of special characters that define a pattern. My most frequent example of using <strong>sed<\/strong> is replacing text in a file. The syntax for replacing text looks like this: <code>s\/originaltext\/newtext\/<\/code>. The <code>s<\/code> tells <strong>sed<\/strong> to perform text replacement or swap occurrences of text. Provide the original text and new text between slashes.<\/p>\n\n\n\n<p>This syntax will only replace the first occurrence of <code>originaltext<\/code> on each line. To replace every occurrence, even if the original text appears more than once on a line, append <code>g<\/code> to the end of the expression, like this: <code>s\/originaltext\/newtext\/g<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"editing-files\">Editing files<\/h2>\n\n\n\n<p>To use this with <strong>sed<\/strong>, specify this regular expression with the <code>-e<\/code> option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed -e 's\/originaltext\/newtext\/g'<\/code><\/pre>\n\n\n\n<p>For example, let\u2019s say I have a Makefile for a program called <strong>game<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.PHONY: all run clean\n\nall: game\n\ngame: game.o\n    $(CC) $(CFLAGS) -o game game.o $(LDFLAGS)\n\nrun: game\n    .\/game\n\nclean:\n    $(RM) *~\n    $(RM) *.o\n    $(RM) game<\/code><\/pre>\n\n\n\n<p>The name <strong>game<\/strong> isn\u2019t very descriptive, so I might choose to rename it <strong>life<\/strong>, for Conway\u2019s Game of Life. Renaming the <code>game.c<\/code> source file to <code>life.c<\/code> is easy enough with the <strong>mv<\/strong> command, but now I need to modify the Makefile to use the new name. I can use <strong>sed<\/strong> to change every occurrence of <strong>game<\/strong> to <strong>life<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed -e 's\/game\/life\/g' Makefile\n.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>This prints the <strong>sed<\/strong> output to the screen, which is a good way to check if the text replacement will do what you want. To make these changes to the Makefile, first, make a backup of the file, then run <strong>sed<\/strong> and save the output to the original filename:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cp Makefile Makefile.old\n$ sed -e 's\/game\/life\/g' Makefile.old &gt; Makefile<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"edit-in-place\">Edit in place<\/h2>\n\n\n\n<p>If you are confident that your changes are exactly what you want, use the <code>-i<\/code> or <code>--in-place<\/code> option to edit the file in place. However, I recommend adding a backup filename suffix like <code>--in-place=.old<\/code> to save a copy of the original file in case you need to restore it later:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed --in-place=.old -e 's\/game\/life\/g' Makefile\n$ ls Makefile*\nMakefile  Makefile.old<\/code><\/pre>\n\n\n\n<p><em>This article is modified 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>Here\u2019s a neat command line trick to make changes to a text file.<\/p>\n","protected":false},"author":33,"featured_media":3522,"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-9447","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\/9447","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=9447"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9447\/revisions"}],"predecessor-version":[{"id":9449,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9447\/revisions\/9449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3522"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}