How to extract a number from a string using Bash example

The purpose of this tutorial is to show how to extract a number from a string using Bash – that is, either in a Bash script or from the Linux command line. It is common to use Bash as a utility to process strings or other textual data, so it is well equipped for the task of separating numbers out of a string. There are numerous ways to achieve this; see some of the examples below to get started.

In this tutorial you will learn:

  • How to extract one or more numbers from a string using Bash
How to extract a number from a string using Bash example
How to extract a number from a string using Bash example
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Extract Number From String – Bash Examples




These methods can either be used inside of a Bash script or right from your command line terminal. We will be presenting the methods as they would be used from command line, but feel free to adapt them as necessary to conform to your Bash script.

  1. The first method we will look at to extract a number is to use the tr command.
    $ NUMBER=$(echo "The store is 12 miles away." | tr -dc '0-9') ; echo $NUMBER
    12
    
  2. Another method is to use the sed command.
    $ NUMBER=$(echo "The store is 12 miles away." | sed 's/[^0-9]*//g') ; echo $NUMBER
    12
    
  3. In the next method we will use only a built in Bash function:
    $ STRING="The store is 12 miles away." ; echo "${STRING//[!0-9]/}"
    12
    

    Or a similar method:

    $ STRING="The store is 12 miles away." ; echo "${STRING//[^0-9]/}"
    12
    
  4. The grep command can also be used to extract a number from a string:


    $ NUMBER=$(echo "The store is 12 miles away." | grep -o -E '[0-9]+') ; echo $NUMBER
    12
    
  5. There are also multiple ways that this can be achieved with the awk command. One such example:
    $ NUMBER=$(echo "The store is 12 miles away." | awk -F'[^0-9]*' '$0=$2') ; echo $NUMBER
    12
    
  6. Note that the examples above will also work when extracting multiple numbers from a string, but they will combine the numbers. The following example will extract multiple numbers from a string while also separating each value with a space.
    $ NUMBERS=$(echo "The store is 12 miles away, and a 24 minute drive at 30 miles per hour. | "tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g') ; echo $NUMBERS
    12 24 30
    

Closing Thoughts




In this tutorial, we saw how to extract one or more numbers from a string by using Bash on a Linux system. Linux gives us quite a few different command line utilities to achieve this function, and we are also able to use builtin Bash syntax. These examples should be enough to get you started with extracting numbers on either the command line or in a bash script, and if necessary can be easily adapted for various scenarios.



Comments and Discussions
Linux Forum