Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: November 29, 2024
In Linux, removing special characters in text is not uncommon during text analysis. For instance, we can encounter a scenario in which we may need to clean text for applications such as databases.
In this tutorial, we’ll walk through several commands for removing all special characters from Linux text. In particular, we’ll focus on tr, sed, awk, perl, and grep.
In the context of this article, special characters comprise all characters other than alphanumeric characters, for instance, whitespaces, symbols, and control characters to mention a few.
To demonstrate, let’s use the echo command to create a file with text:
$ echo -e "This is a sample text with special characters @#%^&*()_+={}[]|;:'\"<>,.?/~\` and the control characters \x01\x02\x03\x0D\x1A" > sample_text.txt
Above, we create the sample_text.txt file containing text that includes some special characters by adding the -e switch for interpreting character codes in escape sequences like \x01:
$ cat sample_text.txt
�his is a sample text with special characters @#%^&*()_+={}[]|;:'"<>,.?/~` and the control characters
This text output contains control characters. However, these characters are non-printable and meant for text formatting, device control, or special functions like line breaks, making them invisible in normal text output. In addition, control characters can appear in text due to encoding issues or file transfers between different systems.
Now, let’s make the control characters visible:
$ cat -v sample_text.txt
This is a sample text with special characters @#%^&*()_+={}[]|;:'"<>,.?/~` and the control characters ^A^B^C^M^Z
Here, we use the -v option to display the control characters as symbols in the cat output.
We can use the tr command to specify a range that helps us delete all characters except alphanumerics:
$ tr -cd '[:alnum:]' < sample_text.txt > cleaned_sample_text.txt && cat -v cleaned_sample_text.txt && echo
Thisisasampletextwithspecialcharactersandthecontrolcharacters
Let’s analyze the command:
This command deletes all special characters in the text including whitespaces.
Alternatively, we can use the sed command with regular expressions to remove all special characters.
$ sed 's/[^a-zA-Z0-9]//g' sample_text.txt > cleaned_sample_text.txt && cat -v cleaned_sample_text.txt
Thisisasampletextwithspecialcharactersandthecontrolcharacters
Let’s analyze the command:
Therefore, sed is another versatile tool we can use to manipulate text.
We can also utilize the awk command to remove all special characters from a file or stream:
$ awk '{gsub(/[^a-zA-Z0-9]/, ""); print}' sample_text.txt > cleaned_sample_text.txt && cat -v cleaned_sample_text.txt
Thisisasampletextwithspecialcharactersandthecontrolcharacters
Let’s see the breakdown of the awk command:
This command processes the text in sample_text.txt and then displays the desired output. To clarify, awk processes text line by line and can handle complex patterns efficiently.
The perl interpreter is another powerful tool we can use to remove all non-alphanumeric characters:
$ perl -pe 's/[^a-zA-Z0-9]//g' sample_text.txt > cleaned_sample_text.txt && cat -v cleaned_sample_text.txt && echo
Thisisasampletextwithspecialcharactersandthecontrolcharacters
So, perl invokes the perl interpreter:
Finally, echo displays the output on a new line.
Typically, the grep command searches for patterns in text:
$ grep -o '[a-zA-Z0-9]*' sample_text.txt
This
is
a
sample
text
with
special
characters
Above, the -o option instructs grep to output only the alphanumeric sequences with each sequence in a new line. Now, we can combine grep with other commands to filter out special characters.
First, let’s use grep with sed and tr:
$ grep -o '[a-zA-Z0-9]*' sample_text.txt | sed 's/^$/ /' | tr -d '\n' > cleaned_sample_text.txt && cat -v cleaned_sample_text.txt && echo
Thisisasampletextwithspecialcharactersandthecontrolcharacters
To clarify, we can check what each command does:
Here, grep employs the same idea of regular expressions.
In this article, we explored multiple approaches to remove special characters in Linux text. In particular, we looked at how to use tr, sed, awk, perl, and grep.
Each tool offers a unique approach as well as syntax for handling text manipulation. Therefore, we can select which tool to utilize depending on the task complexity, size of the dataset, or even familiarity with the syntax, offering us flexibility.
These utilities are essential for text processing whether we’re cleaning up data files, preparing text for importing into a specific application like a database, or simply formatting output.