How to get a numeric value from a string in bash… here is a solution to the problem.
How to get a numeric value from a string in bash
I
have an xml file and I just want to extract values from strings in the file. One of the solutions I came up with was
cat file.xml |grep -i "mu "| grep -o '[0-9]'
But every number I get is separated by a new line, e.g. 100, I get 1, then a new line, then a 0, and so on. Another solution I came up with is
cat file.xml |grep -i "mu "|cut -d ' ' -f 4| tr '=' ' '|cut -d ' ' -f2|tr '""' ' '|sed -e 's/^ *//g' -e 's/ *$//g'
My question: Is there an easier solution to this problem, I just get a numeric value from a row and don’t care about the field and don’t use the cut
or tr
command?
Solution
Use this egrep:
egrep -o '[0-9]+'