Quote:
Originally Posted by Ashkhan
Unfortunately, sed just ignores the + modifier. I also tried \{1,\} instead but it doesn't work too...
|
No, sed does not ignore the + modifier. The problem is in your regex logic:
You need to realize, that the * in sed is "
greedy". It means that sed will read the pattern from left to right and match as many characters as possible so that the regex can still match the line. More specifically:
the first thing sed sees in your regex is the left
.*. It will try to match as many characters as possible so that the rest of the regex can still match the rest of the line. Therefore , the left
.* will match the string like this: "
something_1234.txt", because then it will still have one digit left to match the
[0-9]\+ expression and the right
.* (the latter does not even need any characters to match). Only then will sed continue with
[0-9]\+, which can at this point only match the last digit, because the first three are already "eaten" by the first
.*. Therefore your sed command will output
Code:
$ echo something_1234.txt|sed 's/.*\([0-9]\+\).*/\1/'
4
To fix this, you must replace the first
.* with something that will not be allowed to eat the digits:
Code:
sed 's/[^0-9]*\([0-9]\+\).*/\1/'
or, for the sake of whoever is going to maintain the code, using the
-r option:
Code:
sed -r 's/[^0-9]*([0-9]+).*/\1/'
If you're fine with just removing everything that's not a digit, I would go with the fine solution mentioned by
sycamorex.