In case you have a large number of data provided in a TextFile, and now you need to remove the specific string character from a TextFile.
It is not worth reading every line for a single word or character and manually removing the string. If you have a small set of data, you can use this approach, and For a large file, we cannot use this sluggish approach.
So, we have to find a way to remove all the strings’ occurrences at once without opening a text file.
How to remove specific Text from a TextFile
Thanks to the Linux terminal, we have many command utilities that can help us to solve this task.
Below we have listed down a few utility commands which you can use to remove strings from a TextFile.
- sed command
- awk command
- echo command
- tr command
- cut command
- : > filename
In this guide, we will be more focused on the sed command.If you know more details about sed command you can refer to the below link
We have created a TextFile for this guide, whose name is “demofile.txt.” In this TextFile, we added the list of words.
$ cat demofile.txt
debian ubuntu archlinux centos debian rhel sed command
How to use sed Command to Remove String
If you want to remove a particular string from this TextFile. For example, in my case, I need to remove debian from this TextFile.
To remove debian from the “demofile,” type the following command.
$ sed -i -e 's/stringname/excludetodelete/' filename.txt
$ sed -i -e 's/debian//' demofile.txt
ubuntu archlinux centos rhel sed command
Now we will explain to you how this command worked.
We have used sed commands with several parameters or flags. A parameter -i used to edit files.
-e is called to be expression script this will be executed the script.
Inside the Quote” s is used for separating every file rather than a single line long text, inside the forward-slash (/) you need to pass the string name which you want to remove, In between double forward-slash(//) need to provide a word which you want to keep.
In the end you need to provide a filename.
How to use sed Command to Remove Multiple Strings
When you have the requirement to remove multiple strings from a single TextFile, you can again leverage the sed command.
For example, we will use the same “demofile.txt”, and we want to remove multiple strings from the TextFile like “debian” and “ubuntu.“
$ sed -i -e 's/\(string\|string\)/excludestring/' filename.txt
$ sed -i -e 's/\(debian\|ubuntu\)/linuxiscool/' demofile.txt
archlinux centos rhel sed command
From the output you can see we able to remove multiple string at once.
Now let’ understand how this command worked when you pressed the enter.
We have used -i parameters to edit files, and -e parameters stand for the expressions to execute.
Inside the Quote” s is used for separating every file rather than a single line long text with the delimiters, now we need to pass the string or text inside the parenthesis () along with backward slash \ and use pipe (|) to add another string.
To exclude the string that we don’t want to remove while executing the script, pass the string name inside the // forward-slash.
In the end, you need to provide a filename or directory where a file is present.
I hope so you able to achieve your task using the sed command. If you need any assistance from our side, please comment down below, and If you know the other method, please with us.