Reputation: 331
I got a string which looks like this:
"abcderwer 123123 10,200 asdfasdf iopjjop"
Now I want to extract numbers, following the scheme xx,xxx where x is a number between 0-9. E.g. 10,200. Has to be five digit, and has to contain ",".
How can I do that?
Thank you
Upvotes: 5
Views: 23460
Reputation: 6555
The following example using your input data string should solve the problem using sed.
$ echo abcderwer 123123 10,200 asdfasdf iopjjop | sed -ne 's/^.*\([0-9,]\{6\}\).*$/\1/p'
10,200
Upvotes: 0
Reputation: 19189
Check out pattern matching and regular expressions.
Links:
and as mentioned above, one way to utilize pattern matching is with grep. Other uses: echo supports patterns (globbing) and find supports regular expressions.
Upvotes: 1
Reputation: 212654
A slightly non-typical solution:
< input tr -cd [0-9,\ ] | tr \ '\012' | grep '^..,...$'
(The first tr removes everything except commas, spaces, and digits. The second tr replaces spaces with newlines, putting each "number" on a separate line, and the grep discards everything except those that match your criterion.)
Upvotes: 0
Reputation: 360683
In pure Bash:
pattern='([[:digit:]]{2},[[:digit:]]{3})'
[[ $string =~ $pattern ]]
echo "${BASH_REMATCH[1]}"
Upvotes: 5
Reputation: 189936
Simple pattern matching (glob patterns) is built into the shell. Assuming you have the strings in $*
(that is, they are command-line arguments to your script, or you have used set
on a string you have obtained otherwise), try this:
for token; do
case $token in
[0-9][0-9],[0-9][0-9][0-9] ) echo "$token" ;;
esac
done
Upvotes: 1
Reputation: 455440
You can use grep
:
$ echo "abcderwer 123123 10,200 asdfasdf iopjjop" | egrep -o '[0-9]{2},[0-9]{3}'
10,200
Upvotes: 11