Belajar 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

Wagiman Wiryosukiro

Petani Sistem Informasi, tukang las plugin & themes Wordpress. Co-Founder SistemInformasi.biz. Saat ini aktif sebagai Developer & kontributor di OpenMandriva Linux.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: