6 Ways to Find and Replace Text in the Linux Terminal

Editing a configuration file or updating a large document by going through each line manually can take a lot of time. However, with command-line tools like sed, awk, Vim, and others, you can make the search and replace process fast and efficient.




Use sed Command

Want quick text replacements? Use the sed command. With sed, you can look for and modify text within files, handling everything from simple substitutions to complex text manipulations.

Let’s say you have a file named example.txt that contains the word “Linux,” and you decide to replace it with “Windows.” To do this, run:

sed 's/Linux/Windows/g' example.txt

Finding and replacing specific word from text file using sed command.

Here, the s flag stands for substitute, and the g flag allows sed to substitute all pattern occurrences on each line.

The previous command doesn’t directly modify the original file; it only shows the modified output in the terminal. However, if you want to manipulate the original file directly, use the -i flag:


sed -i 's/Linux/Windows/g' example.txt

The -i flag makes the changes directly in the file instead of only displaying the output.

Want to back up your file before making changes? Add an extension to the -i flag:

sed -i.bak 's/Linux/Windows/g' example.txt

This creates a backup file called example.txt.bak before making any changes.

But what if you want your original file to remain unchanged? You can create a new file and save the modified output there using the redirection operator.

For instance, to send the output to a separate file, use:

sed 's/Linux/Windows/g' example.txt > file.txt

Using complex regex patterns with sed gives you control over what to replace in larger files with repetitive content. To replace multiple instances with a single command, run:

sed -i 's/Linux\|Ubuntu/Windows/g' example.txt


A trick I often use with sed is switching to a different delimiter if my search or replacement strings contain forward slashes. For example, when I’m working with file paths, I like to use the @ symbol as the delimiter:

sed 's@old/path@new/path@g' file.txt

Finding and replacing text that contains path using specified delimiter.

This makes the command easier to read and avoids the need for many escape characters.

Replace Text Using awk Command

Like sed, awk also performs simple to advanced text manipulation. I often use awk to handle structured data, especially with CSV files, where calculations on selected fields might be needed.

Although awk is well-known for managing structured data, it’s also effective for quick find-and-replace tasks. For example, to replace specific text in a file, you could use:

awk '{gsub(/Windows/, "Linux"); print}' file.txt > new_file.txt


Replacing and viewing file content using awk command.

Here, the gsub function performs a global substitution, replacing every occurrence of “Windows” with “Linux” across each line in the file. Since awk doesn’t modify files directly, we redirect the output to new_file.txt to capture the changes.

I also use awk to clean up data exports, especially from spreadsheets, where extra formatting can complicate data processing. Many spreadsheet exports include quotes around values, which can disrupt data processing in other applications.

You can remove all double quotes from the first column of a CSV file using this command:

awk -F, '{gsub(/"/, "", $1); print}' data.csv > cleaned_data.csv


Here, gsub removes every double quote from only the first column, and the output is redirected to cleaned_data.csv for further use.

Substitute Text With Vim Ex Mode

Want quick text transformations without opening the full editor? Try Vim (Vi Improved), a text editor with an Ex mode that allows you to perform find-and-replace operations directly from the command line.

To begin, open your file with Vim:

vim filename.txt

Then, enter into Ex mode by pressing Esc, and then use the (:) key. This will prompt you with a colon (:) at the bottom of the terminal, indicating that you’re in Ex mode. To find and replace text, use this command:

:%s/macOS/Linux/g

Replacing specific string value with another using Vim Ex mode.


This command replaces all occurrences of “macOS” with “Linux” throughout the entire file. The % symbol specifies that the command should apply to the whole file, and the g flag at the end ensures all occurrences on each line are replaced, not just the first one.

To replace only the first occurrence on each line, simply omit the g flag:

:%s/macOS/Linux/

Need to replace text within a specific range of lines? Specify the line range. For instance, to replace text from line 3 to line 10:

:3,10s/macOS/Linux/g

One habit I follow when making file changes is to confirm each replacement. This approach allows careful, line-by-line edits.

To get a prompt for each replacement, add c at the end of the command:

:%s/macOS/Linux/gc

Using Bash Script

Sometimes, you might need to perform a series of find-and-replace operations across multiple files or directories. For these cases, using Bash scripts can automate your tasks that would take a long time to do manually.


Let’s consider a simple script that replaces an old string with a new string in multiple text files within the current directory:

#!/bin/bash
for file in *.txt; do
    sed -i 's/Windows/Linux/g' "$file"
done
Creating bash script file for searching and replacing text of file in terminal.

To use this script, first save it to a file (e.g., bashexample.sh), then make the file executable by running:

chmod +x bashexample.sh

Then, execute the script with:

./bashexample.sh

The for loop iterates over all text files in the current directory and uses sed to replace text within each file. You can also modify this script to handle different patterns or file types as needed.

Replace Text With Python Script

With Python’s advanced text-processing capabilities, you can perform find-and-replace tasks on files directly from the terminal. Python scripts are helpful when you want more complex operations, such as handling multiple files with conditional logic.


First, create a new Python file. You can use any text editor, such as nano or Vim:

vim textfile.py

Next, add the following Python script to the file:

#!/usr/bin/env python
import sys
with open(sys.argv[1], 'r+') as f:
    content = f.read().replace('Windows', 'Linux')
    f.seek(0)
    f.write(content)
    f.truncate()

After saving the file, make it executable by running:

chmod +x ./testfile.py

Then, run the script by passing the target file as a command-line argument:

python3 ./testfile.py input.txt

This script reads the content of input.txt, replaces occurrences of the old text with the new text, and saves the changes directly to the file.

Replace Set of Characters With tr Command

Have you ever needed to replace just a single character with another character in the entire file? If yes, then you need to try the tr command. This command is perfect for simple character replacements or deletions.

For instance, I often use it to convert specific characters in a text file to uppercase or lowercase. To replace all lowercase a’s and d’s with uppercase A’s and D’s in a text file, run:

tr 'a,d' 'A,D' < example.txt


Replacing multiple sets of characters with tr command.

Remember that tr works on characters rather than strings or words, so it’s best suited for simple character replacements rather than more complex patterns.

You can also clean up whitespace or standardize line endings by running this:

cat messy_file.txt | tr '\r' '\n' > clean_file.txt

This converts Windows-style line endings (\r\n) to Unix-style line endings (\n), which is helpful when working with files across different operating systems.

Using perl

Perl, a high-level programming language, can also be used directly in the terminal to find and replace text, making it a great alternative to sed. Its syntax is similar to sed’s, but it offers more advanced features.


Personally, I really liked Perl because of its particularly strong regular expression capabilities, which make it a great choice for complex find-and-replace operations.

You can replace any word in a file using this command:

perl -pi -e 's/Linux/Windows/g' file.txt

Here, the -pi flags tell perl to edit the file in place, and the -e flag allows you to pass an expression directly. This command modifies file.txt by replacing “Linux” with “Windows” throughout the file.


For simple substitutions, command-line tools like sed, awk, or perl work well. Choose a Bash or Python script if you need to perform a more complex substitution across multiple files or with additional logic.

Leave a Comment