{"id":1634,"date":"2021-12-16T13:45:55","date_gmt":"2021-12-16T10:45:55","guid":{"rendered":"https:\/\/files2.tojikon.net\/files-cloud\/2021\/03\/30\/linux-replace-text-string-in-file-guide\/https:\/\/files2.tojikon.net\/files-cloud\/2021\/03\/30\/linux-replace-text-string-in-file-guide\/"},"modified":"2021-12-16T13:45:55","modified_gmt":"2021-12-16T10:45:55","slug":"linux-replace-text-string-in-file-guide","status":"publish","type":"post","link":"https:\/\/tojikon.net\/en\/1634-linux-replace-text-string-in-file-guide\/","title":{"rendered":"Linux: replace text string in file [Guide]"},"content":{"rendered":"<section id=\"related_posts\">\n<div class=\"block-head\">\n<h3>Related Articles<\/h3>\n<\/div>\n<\/section>\n<p>Editing text files on Linux by hand can be tedious. That\u2019s why it\u2019s good to know how to replace text strings in files using the command line quickly. If you\u2019re new to Linux and don\u2019t know how to do it, we can help! Follow along as we show you how to replace a text string in a file on Linux!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-398213 size-full\" src=\"https:\/\/files2.tojikon.net\/files-cloud\/wp-content\/uploads\/2021\/12\/linux-replace-text-string-in-file-guide.png\" alt=\"Linux: replace text string\" width=\"1200\" height=\"1149\"\/><\/p>\n<h2>Replace text string in file \u2013 sed<\/h2>\n<p>The sed tool is the best way to replace a text string in a file on Linux. The reason? It\u2019s straightforward to use and does its job very well. Sed is usually pre-installed on 99% of Linux operating systems, so there\u2019s no need to install it to use it.<\/p>\n<p>Replacing a text string in a file with Sed is done with the <strong>sed -i\u00a0<\/strong>command. Here\u2019s how to use it. First, open up a terminal window on the Linux desktop. You can open up a terminal window by pressing\u00a0<strong>Ctrl + Alt + T<\/strong> on the keyboard.<\/p>\n<p>Once the terminal window is open and ready to use, write out <strong>sed -i\u00a0<\/strong>in the terminal prompt. This is the start of the replacement command.<\/p>\n<pre>\nsed -i\n<\/pre>\n<p>After writing out the\u00a0<strong>sed -i\u00a0<\/strong>command, add in a \u2018 quote mark. This quotation mark is essential, as all text being replaced with <strong>sed -i\u00a0<\/strong>needs to start after this mark.<\/p>\n<pre>\nsed -i '\n<\/pre>\n<p>Following the first\u00a0<strong>\u2018<\/strong> mark, write <strong>s\/<\/strong>. The\u00a0<strong>s\/\u00a0<\/strong>goes directly before the existing text you wish to replace using\u00a0<strong>sed<\/strong>.<\/p>\n<pre>\nsed -i 's\/\n<\/pre>\n<p>Now that the\u00a0<strong>s\/\u00a0<\/strong>is written into the terminal command-line prompt, it is time to tell sed what text to replace in the file. In this example, we\u2019ll replace the word \u201capple.\u201d\u00a0<\/p>\n<pre>\nsed -i 's\/apple\n<\/pre>\n<p>When the text we want to replace is written into the command, the next step is to write in the new text that will replace it. In this example, we\u2019ll replace \u201capples\u201d with \u201coranges.\u201d<\/p>\n<pre>\nsed -i 's\/apples\/oranges\n<\/pre>\n<p>Once the text we want to replace is written into the command (oranges), close off the command with <strong>g\/<\/strong>. The <strong>g\/<\/strong> tells\u00a0<strong>sed<\/strong> to replace all text instances (apples) with the new text (oranges). It should look like the example below.<\/p>\n<p>Note: if you do not want to replace every text in the file (apples to oranges, for example), remove the <strong>g<\/strong> and have it run as <code>sed -i 's\/apples\/oranges\/'<\/code> instead.<\/p>\n<pre>\nsed -i 's\/apple\/orange\/g'\n<\/pre>\n<p>Finally, tell\u00a0<strong>sed\u00a0<\/strong>what file the text is in that needs replacing. In this example, fruit.txt in the home directory is the target.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-398213 size-full\" src=\"https:\/\/files2.tojikon.net\/files-cloud\/wp-content\/uploads\/2021\/12\/linux-replace-text-string-in-file-guide.png\" alt=\"\" width=\"1200\" height=\"1149\"\/><\/p>\n<pre>\nsed -i 's\/apples\/oranges\/g' ~\/fruit.txt\n<\/pre>\n<p>Press the\u00a0<strong>enter key\u00a0<\/strong>to execute the command and replace your text string in the file.<\/p>\n<h2>Replace text string in file \u2013 Perl<\/h2>\n<p>Another way to replace a text string in a file on Linux is with the Perl programming language. Perl is used for text processing a lot, so naturally, it can swap out text strings in files and is perfect for this use case.<\/p>\n<p>To start, ensure you have Perl installed on your Linux PC. Most Linux operating systems come with Perl installed. However, if you do not have it, check your operating system\u2019s help page for information on how to get it working.<\/p>\n<p>Using Perl to replace text strings in a file requires the terminal. Open up a terminal on the Linux desktop by pressing\u00a0<strong>Ctrl + Alt + T\u00a0<\/strong>on the keyboard. Or, search for \u201cTerminal\u201d in the app menu and launch it that way.<\/p>\n<p>Once the terminal window is open and ready to use, start by typing out\u00a0<strong>perl -pe\u00a0<\/strong>in the command-line prompt. The\u00a0<strong>perl -pe\u00a0<\/strong>command is what is needed to replace a text string in a file.<\/p>\n<pre>\nperl -pe\n<\/pre>\n<p>Upon writing out\u00a0<strong>perl -pe\u00a0<\/strong>in the terminal prompt, you will need to start with the first\u00a0<strong>\u2018\u00a0<\/strong>quote mark. This mark tells Perl where the text replacement area is in the command.<\/p>\n<pre>\nperl -pe '\n<\/pre>\n<p>Following the first\u00a0<strong>\u2018\u00a0<\/strong>quote mark, add\u00a0<strong>s\/<\/strong>, followed by the string of text you wish to replace, and another <strong>\/<\/strong>. For example, to replace \u201capples\u201d in the fruit.txt file, you\u2019d write out the following text.<\/p>\n<pre>\nperl -pe 's\/apples\/\n<\/pre>\n<p>After writing out the word(s) you wish to replace, add in the word that will replace the existing text. For example, if you want to replace \u201capples\u201d in \u201cfruit.txt\u201d with \u201coranges,\u201d add in \u201coranges\u201d after <code>perl -pe 's\/apples\/<\/code> so it looks like <code>perl -pe 's\/apples\/oranges\/<\/code>.<\/p>\n<pre>\nperl -pe 's\/apples\/oranges\/\n<\/pre>\n<p>Once both strings of text are in the command, you\u2019ll need to specify the input file that Perl uses. For example, if you want to replace text in the \u201cfruit.txt\u201d file, you\u2019ll need to specify it in the command.<\/p>\n<p>Note: in this example, the \u201cfruit.txt\u201d file is in the home directory (~\/). Be sure to replace \u201c~\/fruit.txt\u201d with your text file\u2019s location for the command to work.<\/p>\n<pre>\nperl -pe 's\/apples\/oranges\/' ~\/fruit.txt\n<\/pre>\n<p>Now that the input file (the file you\u2019re modifying with Perl) is added to the command, the command should look like it does below.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-398209 size-full\" src=\"https:\/\/files2.tojikon.net\/files-cloud\/wp-content\/uploads\/2021\/12\/linux-replace-text-string-in-file-guide-2.png\" alt=\"\" width=\"1200\" height=\"755\"\/><\/p>\n<pre>\nperl -pe 's\/apples\/oranges\/' ~\/fruit.txt &gt; \/tmp\/output.txt;cat \/tmp\/output.txt &gt; ~\/fruit.txt\n<\/pre>\n<p>When the command above is run, the text string will be replaced with the new text you specified. In our example, we replaced \u201capples\u201d with \u201coranges.\u201d To view the changes, enter the command below.<\/p>\n<pre>\ncat fruit.txt\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Related Articles Editing text files on Linux by hand can be tedious. That\u2019s why it\u2019s good to know how to replace text strings in files using the command line quickly. If you\u2019re new to Linux and don\u2019t know how to do it, we can help! Follow along as we show you how to replace a &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1635,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[33],"_links":{"self":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/posts\/1634"}],"collection":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/comments?post=1634"}],"version-history":[{"count":0,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/posts\/1634\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/media\/1635"}],"wp:attachment":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/media?parent=1634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/categories?post=1634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/tags?post=1634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}