Reputation: 2374
I am trying to extract the numbers from lines like:
<foo> 34Kb data <foo>
<foo> 2Kb data <foo>
where I am also extracting other parameters, so it is important to match the group of numbers before Kb data
and to preferably use sed
.
I have tried:
sed -r 's/.*([0-9]+)Kb data.*/\1/'
And other combinations, but they only gave me the last digit for a group of digits.
Thank you!
Upvotes: 2
Views: 313
Reputation: 67968
This is because of your *
greedy operator.But sed does not support *?
non greedy operator.so instead use grep -P
grep -P '.*?([0-9]+)Kb data.*'
or
grep -P '\d+(?=Kb)'
simply.See demo.
https://regex101.com/r/oL9kE8/13
Or
sed -r 's/[^0-9]*([0-9]+)Kb data.*/\1/'
Upvotes: 3
Reputation: 44023
You can use
sed -r 's/.*\b([0-9]+).*/\1/'
\b
matches a word boundary (beginning or end of a word).
Upvotes: 4