How do I grab only the number from a command line output.
For example, I have a program which on being run outputs a string like
submitted batch with job number 12345
I would like to grab only the number
12345 and pipe it to another program.
The simplest form to get only the number from the string you have provided is as follows.
string="submitted batch with job number 12345" echo $string | grep -o '[0-9]\+'
This will fail if the string or output of your program does include a second number group like
string="submitted 1 batch with job number 12345"
In that case, you would could use
sed to e.g. search for the string number and only match for numbers after that occurrence.
echo $string | sed -n 's/^.*number\ \([0-9]\+\)/\1/p'