Monday, April 23, 2012

Bash Shell: Extract Digits From a String

How do I extract digits only from a given string under Bash shell?

You can use the sed, grep and other shell utilities as follows:
 
grep -o "[0-9]" <<<"input"
grep -o "[0-9]" <<<"$var"
grep -o "[0-9]" <<<"test123"
grep -o "[0-9]" <<<"1Th5is is a test. 5"
var="foo1bar2"
output=$(grep -o "[0-9]" <<<"$var")
echo "$output"
grep -oE "[[:digit:]]{1,}" input.file
 

No comments:

Post a Comment