Keeping in view the importance of sed command; our today’s guide will explore several ways to remove special characters using sed command in Ubuntu.
The syntax of sed command is written below:
Syntax
Special characters may sometimes be a need of the content that is written in a text file but if they are used unnecessarily, they will make the file messy and there are chances the reader may not pay attention, thus resulting in a purposeless document.
How to use sed to remove special characters in Ubuntu
This section will briefly describe the ways to remove special characters from a text file using sed; it depends on number of characters in your file that you want to remove; there can be two possibilities while removing the characters from a file, either you want to remove a single special character, or you want to remove multiple characters at once. From these possibilities indicated above, we have extended this section to two methods that will address both possibilities:
Method 1: How to remove a single character using sed
Method 2: How to remove multiple characters at once using sed
The first method addresses the first possibility, and the second possibility will be discussed in Method 2, let’s dig into them one by one:
Method 1: How to remove a single special character using sed
We have created a text file “ch.txt” that contains few special characters on different lines; the content inside the file is displayed below:
You can notice that the content inside “ch.txt” is difficult to read; For instance, we want to remove character “#” from the text file; for this, we have to use the following command to remove “#” from the whole document:
Moreover, if you want to remove the special character from specific line; for that, you must insert the line number alongside “s” keyword as the below mentioned command will remove “#” from line number 3 only:
Method 2: How to remove multiple characters at once using sed
Now we have another file “file.txt” that contains more than one type of character and we want to remove them in a single go. in this method the syntax is changed little bit from above command; For example, we have to remove five characters “#$%*@” from “file.txt”;
Firstly, look at the content of “file.txt” as the words are interrupted by these characters;
the command stated below will assist to remove all these special characters from “file.txt”:
Here we can draw another example, let’s say we want to remove only a few characters from specific lines.
We have created a new file and the content of the “newfile.txt” is shown below:
For this, we have written command that will delete “#@” and “%*” from lines 2 and 3 of “newfile.txt” respectively.
The sed command used in above methods will display the result only on the terminal rather than applying the changes in the text file: for that, we must use the “-i” option of sed command. It can be used with any sed command and the changes will be made to the file instead of printing on the terminal.
Conclusion
Apparently, the sed command acts as a usual text editor but it has a far more extensive list of actions as compared to other editors. You have to just write a command and the changes will be made automatically; this feature attracts the Linux enthusiasts or the users who prefer terminal over GUI. Following the advantageous functionalities of sed; our guide is focused on removing special characters from the text file. If we compare only this feature of sed command with other editors, you have to search for characters throughout the file and then removing them one by one is a tedious process. On the other hand, sed performs the same action by writing a single line command on terminal.