Bash Shell: Extract Digits From a String
If you want to extract digits from a string in bash, you can use the following methods: Using grep: string=”abc123def456″ digits=$(echo $string | grep -o ‘[0-9]\+’) echo $digits Output: 123456 Using sed: string=”abc123def456″ digits=$(echo $string | sed ‘s/[^0-9]*//g’) echo $digits Output: 123456 Using awk: string=”abc123def456″ digits=$(echo $string | awk ‘/[0-9]+/{ gsub(/[^0-9]+/, “”, $0); print $0 … Read more