


$ cat test.txtįind and Replace Word in Linux Using Sed CommandĪs you can see, the first two occurrences of the word “ LinuxShellTips” have been replaced with the phrase “ this site”. We can now use the cat command to preview the above-proposed changes to our test file to note if any substantial edits took place. With reference to the above sed command usage and syntax, we can replace our test file’s “ LinuxShellTips” term with “ this site” term as follows: $ sed -i 's/LinuxShellTips/this site/' test.txt The syntax of the sed command we will be using to accomplish this simple task is as follows: $ sed -i 's///' Find and Replace the First Occurrences of Text, Word, or String in Fileįrom our created test file, we are going to update all the instances of the word “ LinuxShellTips” with the alternative “ this site”. The sed command offers a quick, easy, and timeless approach in finding and replacing targeted text, words, or string patterns in a file. The power of this stream editor is in its easiness in accomplishing basic input streams transformation. View Text File in Linux Using Sed to Find and Replace Text, Word, or String in a File We will be using the cat command to flexible note the changes on our created test file. Let this file be on the same path as your terminal instance. Creating the Test Text File in LinuxĬreate a text file with a name like “ test.txt” and populate it with some text, word, or string characters. A solution to this problem lets you handle nagging issues like updating the “ /etc/apt/sources.list” file after a successful Linux system upgrade. One such problem is the need to find and replace text, word, or string in a file especially when you are in a server environment. If you manage to take a deeper glimpse inside the ecosystem of the Linux operating system environment, you will discover that its built-in commands are sufficient enough to solve most of our computing problems.
