AdamAL
AdamAL

Reputation: 1681

Why is sed matching minimum and not maximum string?

I am trying to extract the numbers (0.000500) from a bunch of files alá eta_x2-0.000500. I would think that below line would do that, but i get only 0 and not 0.000500.

How can I get the maximum match?

find eta* | sed 's/.*\([0-9.]\+\)/\1/g'

Upvotes: 0

Views: 1375

Answers (4)

divesh premdeep
divesh premdeep

Reputation: 1070

This should work -

find eta* | sed 's/[^-]*-\([0-9]*\.[0-9]*\)/\1/'

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246847

Don't need sed

find eta* | grep -oP '(?<=-)[\s.]+'
find eta* | cut -d'-' -f 2

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208495

The .* is greedy, so it is going to match as many characters as possible. In this case the .* will match eta_x2-0.00050 with just the final 0 matched in your group.

Normally the answer here would be to just use a non-greedy match using .*?, but I don't think sed supports that.

You should be able to get this to work by requiring that there is one non-digit character before you start matching, this way the .* will have to stop before consuming the digits:

sed 's/.*[^0-9.]\([0-9.]\+\)/\1/g'

Of course if you know the digits you want will be immediately after a -, you can replace [^0-9.] with - and it will work the same way.

Upvotes: 2

BMW
BMW

Reputation: 45243

add - in sed before the last numbers, and g is useless.

find eta* | sed  's/.*-\([0-9.]\+\)/\1/'

Upvotes: 1

Related Questions