

You can easily verify that the said changes have only occurred on line number 2 of our file and not in the whole text. The successful execution of this command will show you the changes that have occurred because of running this command on your terminal. In this example, we wanted to replace sed with replace in line number 2 of our file as shown in the following image: Also, you need to replace LineNum with the line number of the particular line in which you want to make the replacement. In this scenario, I have replaced FindWord sed and ReplaceWord with replace.

Moreover, you also need to replace “file” with the name of the file where you want to make these replacements. Here, you need to replace FindWord with the word that you wish to replace and ReplaceWord with the word to be replaced. $ sed ‘LineNum s /FindWord /ReplaceWord /’ file.txt The syntax of achieving this goal is mentioned below: You can use the sed command for replacing all the occurrences of a given word in a text file. Using the sed Command for replacing all Occurrences of a Given Word: Now launch the terminal in Linux Mint 20 as shown in the following image:Īfter launching the terminal in Linux Mint 20, you can go through all the examples mentioned below one by one. Save this text file by pressing Ctrl +S and then close it. For this particular example, I have named it as sed.txt.ĭouble click on this file to open it and type any random text in it as shown in the image below. Once the text file has been created, give a suitable name to it. Now create a new text file in the Home directory by right-clicking anywhere over there and then choosing the New Document option from the cascading menu and Empty document option from the sub-cascading menu. For creating a text file, the following steps should be performed:Ĭlick on the File Manager icon located on your Linux Mint 20 taskbar: Secondly, we will create the file in the Home directory so that we do not have to specify any complex paths while accessing this file. However, you can even create a Bash file or any other file of your choice. Creating a Text File for Demonstration:įor demonstrating the usage of the sed command, we would like to work with a text file. Let us see, how this command works in different scenarios. Now we are going to show you some very interesting examples for depicting the usage of the sed command in Linux. You can also use any other flavor of Linux if you wish to. Note: We will be using Linux Mint 20 for demonstrating the usage of the sed command to replace a string in a file. We will also throw light on the different scenarios in which this command can be used. Therefore, in this article, we will walk you through the method of using the sed command to replace a string in a text file.

However, at times, you need a way to find and replace the words within a file via the terminal. Most of the text editors do provide different GUI based methods to perform these modifications. Whenever we are working with any sort of files, it is a very common practice to make modifications to those files by finding and replacing words.
