1. Introduction
Shell scripting is a powerful tool for automating tasks and manipulating data within the command-line environment. Among the many tasks it can perform, a common requirement is processing strings by removing numeric characters.
String manipulation involves manipulating and transforming textual data. With a variety of tools at our disposal, such as grep, sed, awk, and more, we can effectively modify strings based on specific requirements.
In this tutorial, we’ll explore different methods to strip numeric characters from a string using various shell scripting tools.
2. Using the sed Command
The sed command is a powerful text-processing tool within shell scripting. It can perform several functions on strings, such as substitutions, deletions, insertions, and more, making it a reliable tool for string manipulation and transformation.
Let’s find out how to use sed to strip numeric characters from a string in the shell:
$ echo "abc123x4yz" | sed 's/[0-9]//g'
abcxyz
Here, we’re using the echo command to print out the input string and then pipe the output to the sed command that uses a regular expression to match and replace numeric characters with an empty string globally with the g flag.
This effectively removes all numeric characters in the input string, and as a result, the output string is abcxyz.
In addition, we can also modify the command above to remove a specific pattern of numeric characters.
As an illustration, let’s remove the numeric sequence 123 from the input string:
$ echo "abc123x4yz" | sed 's/123//g'
abcx4yz
Here, 4 is the only numeric character left in the output string.
3. Using the awk Command
The awk command executes programs written with the AWK programming language. It specializes in processing and manipulating textual data. Consequently, we can write compact but powerful programs or scripts in the form of statements that scan for specific text patterns and perform an action.
Let’s learn how to use awk to extract numeric characters from strings:
$ echo "abc123x4yz" | awk '{ gsub(/[0-9]/,""); print }'
abcxyz
Here, the gsub(/[0-9]/,””) command within awk globally substitutes numeric characters with an empty string, resulting in a string without any digits.
Alternatively, if we have a text file with a list of strings with numeric characters, we can use the cat command to read the contents of the file, then process each line with awk:
$ cat remove_numeric_chatacters.txt | awk '{ gsub(/[0-9]/,""); print }'
abcxyz
baeldung
abxvaas
As a result, all numeric characters are removed from each line of the file.
4. Using the grep Command
grep is a command-line utility in Unix operating systems that searches and filters text within files or input streams.
Although it’s primarily used for pattern matching and filtering lines in text, we can combine grep with regular expressions to eliminate numeric characters from an input string:
$ echo "abc123x4yz" | grep -o '[^0-9]*'
abc
x
yz
In the script above, we’re printing the value of our string mixed with numeric characters and piping it to the grep command that matches and outputs sequences of non-numeric characters.
The -o option prints only matching parts of the string, with each part on a separate line. However, we can pipe the result to the tr command to remove all the line breaks:
$ echo "abc123x4yz" | grep -o '[^0-9]*' | tr -d '\n'
abcxyz
The -d option within the tr command deletes all new lines from grep‘s output.
This method only works with one-liner input strings because, by default, the grep command prints matching parts in separate lines. Moreover, we’re using the tr command to delete all new lines in the input which might distort the original information since the whole output will be on the same line.
5. Using tr Command
The tr command is a Linux command-line utility that translates or deletes characters from standard input and writes the result to standard output.
It comes preinstalled in all Unix distros, so we don’t need to install it.
We can use the tr command to delete specific characters, including numeric values, from an input string.
Let’s find out how to use the tr command to remove numeric characters from a string:
$ echo "abc123x4yz" | tr -d '0-9'
abcxyz
Here, we’re using the -d option to delete all occurrences of numeric characters from the input string.
6. Conclusion
In this article, we’ve looked at different methods of stripping numeric characters from a string in Shell.
Shell scripting provides an array of tools such as sed, awk, tr, and more for efficient string manipulation. Removing numeric characters from a string is a common task and can be accomplished using various methods offered by these tools.
Whether it’s the sed command for substitutions, the tr command for character deletion, or the awk command for pattern scanning, each tool offers its unique way to achieve the desired string transformation.