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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
I am in need of some regex help. I need to pull out a string of numbers for a single line.
The input can look like this, these are 3 separate examples:
o.text text 336 09-Dec-11 13:33:10
o.text text 3350126 09-Dec-11 13:33:10
o.texttext text 30473 09-Dec-11 13:33:10
I need to pull out the middle number, 336, 3350126, 30473
I am close, I think, here is the command I am running:
The input can look like this, these are 3 separate examples:
o.text text 336 09-Dec-11 13:33:10
o.text text 3350126 09-Dec-11 13:33:10
o.texttext text 30473 09-Dec-11 13:33:10
I need to pull out the middle number, 336, 3350126, 30473
It appears you always want the third field, and your fields are delimited by a single blank. Consider using "cut".
test$ while read -a array; do echo ${array[2]}; done < input-file
336
3350126
30473
test$
As for sed, when I tried the given program I got this.
Code:
test$ sed -r 's/([0-9]+).*/\1/' input-file
o.text text 336
o.text text 3350126
o.texttext text 30473
What seems to be happening is that the regex is only matching text from the first digit character on (the backreference). So I decided to match all characters preceeding the first digit outside the backreference.
What seems to be happening is that the regex is only matching text from the first digit character on (the backreference). So I decided to match all characters preceeding the first digit outside the backreference.
Thanks! The first reply with awk is much simpler, however thanks for the regex, I see where I was going wrong. The cut command would not work in my case, there is a variable amount of white space between the strings that where stripped out when I posted this thread, sorry about that.
The cut command would not work in my case, there is a variable amount of white space between the strings that where stripped out when I posted this thread, sorry about that.
That is one reason you should enclose both code and data blocks in code tags. It will preserve the whitespace.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.