kahlo
kahlo

Reputation: 2374

sed: match a group of digits before a word

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

Answers (3)

vks
vks

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

Jotne
Jotne

Reputation: 41446

Here is one way to do it:

awk -F"[^0-9]" '{$1=$1}1' file
      34
      2

Upvotes: 1

Wintermute
Wintermute

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

Related Questions