This tutorial explains how to use the sed command on the command line and the find and replace tool on GUI to find and replace text strings on Linux.
Finding and replacing a text string in the file is one of the most basic text editing operations. All text editors support this operation. You can use the text editor's inbuilt feature or a separate command to find and replace a text string in the file. The first option is good if the file is open for editing. The second option is good if the file is closed. In this tutorial, we will learn about both options. We will use GUI mode to learn about the first option and CLI mode for the second mode.
Finding and replacing text strings on the command line
We can use the sed command to find and replace text strings in the file on the command line. The sed command allows us to search for occurrences of a text string and then replace the text string. It uses the following syntax.
$sed s/exiting_string/new_string/g file_name > new_file_name
The following outlines explain the parameters of the above syntax.
sed :- Main command.
s :- Command's option to search and replace a text string.
exiting_string :- The text string that we want to replace.
new_string :- The new text string that we want to use in the place of the current text string.
g :- Command's option to perform the action in the entire file. You can also perform the replace operation for a specific occurrence of the text string. To replace a specific occurrence of the text string, use the place number of the occurrence. For example, if you want to replace the third occurrence of the text string, use the number 3 at the place of the letter 'g'.
file_name :- File name including the path.
> :- Shell's feature to redirect the output of a command to another command or store the output as a new file.
new_file_name :- Name of the new file. If you want to store the modified file as the new file, you can specify the name of the new file. If you will save the changes in the new file, the command will not change the original file.
Let's take an example.
The following command replaces the second occurrence of the text string 'ocean' with the text string 'sea' in the file 'ocean.txt'.
$sed s/ocean/sea/2 ocean.txt
The following image shows the output of the above command.
The following command replaces all occurrences of the text string 'ocean' with the text string 'sea' and saves the modified file as a new file 'sea.txt'. The original file remains unchanged.
$sed s/ocean/sea/g ocean.txt > sea.txt
The following image shows the output of the above command.
The following command replaces all occurrences of the text string 'ocean' with text string 'sea' in the file 'ocean.txt'.
$sed s/ocean/sea/g ocean.txt
The following image shows the output of the above command.
Finding and replacing text strings on GUI
To find and replace text strings on GUI, open the file in which you want to replace text strings, click the Edit menu item and click the Replace tool. You can also use a combination of shortcut keys to access this tool. Different text editors assign different shortcut keys to access this tool. For example, the Libra office assigns Ctrl + H keys to this tool. To access this tool directly, you can press Ctrl + H keys.
This tool contains some control options and two input boxes: find and replace. Type the text string that you want to remove in the Find box and the text string that you want to add in the Replace box.
You can replace all or some occurrences of the string. To replace all occurrences, click the Replace All button. To replace only the first occurrence, click the Replace button. To replace a specific occurrence, click the Find until you reach the desired occurrence.
By default, the tool performs the replace operation in the forward direction. To perform this operation in the backward direction, click the 'Replace backward' option. You can also use regular expressions to perform more the specific replace operation.
Let's take an example. Suppose we have a file that contains a text string 'sea'. We want to replace this string with the string 'ocean'. To perform this operation, open the file, and press the Ctrl + H keys.
Type the string 'sea' in the Find box and the string 'ocean' in the Replace box and click the Replace All button. The following image shows how to perform this exercise.
You can also try the control options to do more practice.
That's all for this tutorial. In this tutorial, we learned how to use the sed command on CLI and the find and replace tool on GUI to find and replace a text string on Linux.