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.
I want to know how to pull the p and then the number(s) next to it out of these strings:
abcp1de
abcp21de32a
Out of the first one I want "p1" and out of the second one I want "p21"
and yes the strings will be all different, but they will always be just numbers and letters.
I tried to get a grep command that would do this, but I couldn't figure out the right syntax.
Note: If the p is not included and I just get the number that is fine, I would actually prefer that, but it has to be the number that is next to the p.
Thanks! That worked. I am just curious, what does the --color grep option do? I tried the same grep command without that option and it gave me the same results.
The --color option to grep shows the matched string in color. If it shows in color without the option, that's because it's already set in your BASH environment.
Ahhh, lol ya that annoys me when that happens. I type this whole big thing (I type slow) and then I go back and see that someone else already beat me to the punch.
I expanded the test input file to these three lines:
Code:
abcp1de
abcp21de32a
abcp21qp54b
Note that the third line has two p's. The "greedy" nature of sed causes several of the proposed solutions to deliver the numeric string following the second p. I don't think that is what the OP was expecting.
Don't worry, the strings will never have more then one of the same letter in one string at one time. And if it does, my script should error out anyway.
I expanded the test input file to these three lines:
Code:
abcp1de
abcp21de32a
abcp21qp54b
Note that the third line has two p's. The "greedy" nature of sed causes several of the proposed solutions to deliver the numeric string following the second p. I don't think that is what the OP was expecting.
Daniel B. Martin
Even though OP says it does not matter, let's note for completeness how to deal with this.
If there are two sequences on a line, and you want to match only the first one, then do this:
Code:
sed -r 's/[^p]*p([0-9]+).*/\1/'
Instead of searching for any number of characters followed by "p", search for any number of "not p"s
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.