1. Home
  2. Linux

Linux: replace text string in file [Guide]

Editing text files on Linux by hand can be tedious. That’s why it’s good to know how to replace text strings in files using the command line quickly. If you’re new to Linux and don’t 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!

Linux: replace text string

Replace text string in file – sed

The sed tool is the best way to replace a text string in a file on Linux. The reason? It’s straightforward to use and does its job very well. Sed is usually pre-installed on 99% of Linux operating systems, so there’s no need to install it to use it.

Replacing a text string in a file with Sed is done with the sed -i command. Here’s how to use it. First, open up a terminal window on the Linux desktop. You can open up a terminal window by pressing Ctrl + Alt + T on the keyboard.

Once the terminal window is open and ready to use, write out sed -i in the terminal prompt. This is the start of the replacement command.

sed -i

After writing out the sed -i command, add in a ‘ quote mark. This quotation mark is essential, as all text being replaced with sed -i needs to start after this mark.

sed -i '

Following the first  mark, write s/. The s/ goes directly before the existing text you wish to replace using sed.

sed -i 's/

Now that the s/ 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’ll replace the word “apple.” 

sed -i 's/apple

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’ll replace “apples” with “oranges.”

sed -i 's/apples/oranges

Once the text we want to replace is written into the command (oranges), close off the command with g/. The g/ tells sed to replace all text instances (apples) with the new text (oranges). It should look like the example below.

Note: if you do not want to replace every text in the file (apples to oranges, for example), remove the g and have it run as sed -i 's/apples/oranges/' instead.

sed -i 's/apple/orange/g'

Finally, tell sed what file the text is in that needs replacing. In this example, fruit.txt in the home directory is the target.

sed -i 's/apples/oranges/g' ~/fruit.txt

Press the enter key to execute the command and replace your text string in the file.

Replace text string in file – Perl

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.

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’s help page for information on how to get it working.

Using Perl to replace text strings in a file requires the terminal. Open up a terminal on the Linux desktop by pressing Ctrl + Alt + T on the keyboard. Or, search for “Terminal” in the app menu and launch it that way.

Once the terminal window is open and ready to use, start by typing out perl -pe in the command-line prompt. The perl -pe command is what is needed to replace a text string in a file.

perl -pe

Upon writing out perl -pe in the terminal prompt, you will need to start with the first ‘ quote mark. This mark tells Perl where the text replacement area is in the command.

perl -pe '

Following the first ‘ quote mark, add s/, followed by the string of text you wish to replace, and another /. For example, to replace “apples” in the fruit.txt file, you’d write out the following text.

perl -pe 's/apples/

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 “apples” in “fruit.txt” with “oranges,” add in “oranges” after perl -pe 's/apples/ so it looks like perl -pe 's/apples/oranges/.

perl -pe 's/apples/oranges/

Once both strings of text are in the command, you’ll need to specify the input file that Perl uses. For example, if you want to replace text in the “fruit.txt” file, you’ll need to specify it in the command.

Note: in this example, the “fruit.txt” file is in the home directory (~/). Be sure to replace “~/fruit.txt” with your text file’s location for the command to work.

perl -pe 's/apples/oranges/' ~/fruit.txt

Now that the input file (the file you’re modifying with Perl) is added to the command, the command should look like it does below.

perl -pe 's/apples/oranges/' ~/fruit.txt > /tmp/output.txt;cat /tmp/output.txt > ~/fruit.txt

When the command above is run, the text string will be replaced with the new text you specified. In our example, we replaced “apples” with “oranges.” To view the changes, enter the command below.

cat fruit.txt

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.