Tuesday, March 3, 2009

Concatenate only digits from string - bash tr


Just to introduce a good use of Linux tr command; if you need to concatenate the digits from a string, here is a way:

$ echo "Abc123d56E" | tr -cd '[[:digit:]]'
Output:
12356

From tr man pages:

SYNOPSIS

tr [OPTION]... SET1 [SET2]

-c, -C, --complement: first complement SET1
-d, --delete : delete characters in SET1, do not translate

Similarly:

$ echo "Abc123d56E" | tr -d '[[:digit:]]'
Output:
AbcdE

1 comment:

Unknown said...

Thank you for this tip. I never would have figured it out reading the manpage.

© Jadu Saikia www.UNIXCL.com