Quick Links
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 thesed command. With sed, you can look for and modify text within files, handling everything from simple substitutions to complextext 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:

Here, thesflag stands for substitute, and thegflag 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-iflag:

The-iflag 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-iflag:

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 cancreate a new fileand save the modified output there using the redirection operator.

For instance, tosend the output to a separate file, use:
Using complexregex patternswith sed gives you control over what to replace in larger files with repetitive content. To replace multiple instances with a single command, run:
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:

This makes the command easier to read and avoids the need for manyescape characters.
Replace Text Using awk Command
Like sed,awkalso 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:

Here, thegsubfunction 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:
Here,gsubremoves 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 thefull editor? TryVim(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:
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:
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 thegflag:
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:
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, addcat the end of the command:
Using Bash Script
Sometimes, you might need to perform a series of find-and-replace operations across multiple files or directories. For these cases, usingBash scriptscan 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:
To use this script, first save it to a file (e.g., bashexample.sh), thenmake the file executableby running:
Then, execute the script with:
Thefor loop iteratesover 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
WithPython’sadvanced 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’re able to use any text editor, such asnanoor Vim:
After saving the file, make it executable by running:
Then, run the script by passing the target file as a command-line argument:
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 thetr 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:
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:
Thisconverts 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:
Here, the-piflags tell perl to edit the file in place, and the-eflag 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.