How to Use sed to Remove Non-Alphanumeric Characters


Often you may want to use sed to remove non-alphanumeric characters from each line in a file.

You can use the following syntax to do so:

sed "s/[^[:alnum:]-]//g" employee_id.txt

This particular example removes all non-alphanumeric characters from each line in the file named employee_id.txt.

Here is what the various sed commands do:

  • The s command is used to perform a “substitution”
  • The syntax [^[:alnum:]-] matches all characters that are not alphanumeric
  • We then use // to replace each of these matches with nothing
  • We use the g command to specify that these substitutions should be made “globally”, i.e. for all occurrences

The following example shows how to use this syntax in practice.

Example: Use sed to Remove Non-Alphanumeric Characters

Suppose we have a file named employee_id.txt that contains the ID numbers for various employees at some company.

We can use the cat command to view the contents of this file:

Notice that each of the lines contain several non-alphanumeric characters.

We can use the following syntax with sed to remove each of the non-alphanumeric characters from each line:

sed "s/[^[:alnum:]-]//g" employee_id.txt

The following screenshot shows how to use this syntax in practice:

sed remove non-alphanumeric characters

Notice that all non-alphanumeric characters have been removed from each line in the file.

We are only left with characters that are either alphabetical or numeric.

Also note that you can use the > command to save these changes to a new file:

sed "s/[^[:alnum:]-]//g" employee_id.txt > new_id.txt

The following screenshot shows how to use this syntax in practice:

sed remove non-alphanumeric characters from each line in file and save results to new file

Notice that the new file named new_id.txt contains the same information as the employee_id.txt file but with the non-alphanumeric characters removed from each line.

Related Tutorials

The following tutorials explain how to perform other common tasks in sed:

How to Use sed to Insert a Line After Match
How to Use sed to Insert Line at Specific Line Number
How to Use sed to Extract All Lines Containing a Pattern

Leave a Reply