Bash - Extract numbers from a string

I have a line that looks like this:

"abcderwer 123123 10,200 asdfasdf iopjjop"

Now I want to extract the numbers following the pattern xx, xxx, where x is a number between 0-9. For instance. 10200. There must be five digits and must contain ",".

How can i do this?

thank

+5
source share
6 answers

You can use grep:

$ echo "abcderwer 123123 10,200 asdfasdf iopjjop" | egrep -o '[0-9]{2},[0-9]{3}'
10,200
+9
source

In a pure bash:

pattern='([[:digit:]]{2},[[:digit:]]{3})'
[[ $string =~ $pattern ]]
echo "${BASH_REMATCH[1]}"
+4
source

.

:

Bash

SO

, , - grep. : (globbing) find .

+1

( ) . , $* ( script, set , ), :

for token; do
  case $token in
    [0-9][0-9],[0-9][0-9][0-9] ) echo "$token" ;;
  esac
done
0

, , sed.

$ echo abcderwer 123123 10,200 asdfasdf iopjjop | sed -ne 's/^.*\([0-9,]\{6\}\).*$/\1/p'
10,200
0

:

< input tr -cd [0-9,\ ] | tr \  '\012' | grep '^..,...$' 

( tr , , . second tr , "" line, grep , , .)

0

All Articles