ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Unless the OP defines his problem in a clear and definitive way, that's the best I/we can do. The way the OP formulated the problem suggests that it's a single "number" not containing non-numerical characters.

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:

Code:

.*\([0-9]\+\).*

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.

Of course, the solutions by sycamorex and danielbmartin will only work correctly if there's a single set of digits in the string, as they simply delete anything that isn't a number. A string like "1234_something_1234.txt" would end up as "12341234".

But assuming that's ok, then you don't even need to use an external tool. As long as the string is already in a variable, just use simple parameter substitution.

Code:

i='something_1234.txt'
echo "${i//[^0-9]}"

And if you need to be more careful about it:

Code:

i='something_1234.txt'
x=${i%.*}
x=${x##*_}
echo "$x"

These should run faster than any solution using external applications.