LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2012, 04:35 PM   #1
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Rep: Reputation: 10
Regular expression question


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.
 
Old 07-03-2012, 07:00 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
Assuming p is always followed by one or more numbers and occurs only once per string:

You can see the required substring with grep:
Code:
echo abcp21de32a | grep --color p[0-9]*
Or use sed to return the substring:
Code:
echo abcp21de32a | sed 's,.*\(p[0-9]*\).*,\1,'
Hope it helps.
 
2 members found this post helpful.
Old 07-03-2012, 07:13 PM   #3
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
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.
 
Old 07-03-2012, 07:18 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
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.
 
Old 07-03-2012, 07:21 PM   #5
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Ok, that makes sense. Thanks again.
 
Old 07-03-2012, 07:25 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
the man page for grep will tell you about the --color option.

You really want just the numbers AFTER "P", so the sed command should look like this:
(This is a variant of what's already been suggested.)
Code:
sed -r 's/.*p([0-9]+).*/\1/'
Note that I use the -r flag to turn on extended regex rules---thus the () does not have to be escaped. I also use / as the delimiter.
 
1 members found this post helpful.
Old 07-03-2012, 07:39 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by dth4h View Post
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.
To get only the numeric string between "p" and the next alphabetic, rework towheedm's sed slightly:
Code:
sed -r 's/.*p([0-9]*).*/\1/' < $InFile
Daniel B. Martin

Last edited by danielbmartin; 07-03-2012 at 07:47 PM. Reason: Oops, beaten by someone faster on the keyboard!
 
1 members found this post helpful.
Old 07-03-2012, 07:42 PM   #8
paw2012
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
what do I do

now what? this it what i get after rebooting. trying to upgrade to 17.
dropping to debug shell

Last edited by paw2012; 07-03-2012 at 07:47 PM.
 
Old 07-03-2012, 07:47 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by paw2012 View Post
now what? this it what i go after rebooting. trying to upgrade to 17.
dropping to debug shell
???

Please start a new thread---and be sure to tell us what you are doing----by itself, the above makes no sense.
 
Old 07-03-2012, 08:03 PM   #10
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by pixellany View Post
the man page for grep will tell you about the --color option.

You really want just the numbers AFTER "P", so the sed command should look like this:
(This is a variant of what's already been suggested.)
Code:
sed -r 's/.*p([0-9]+).*/\1/'
Note that I use the -r flag to turn on extended regex rules---thus the () does not have to be escaped. I also use / as the delimiter.
Yes this works better. Thanks.


Quote:
Originally Posted by danielbmartin View Post
To get only the numeric string between "p" and the next alphabetic, rework towheedm's sed slightly:
Code:
sed -r 's/.*p([0-9]*).*/\1/' < $InFile
Daniel B. Martin
What does your line do that is different from towheedm's one?


Quote:
Originally Posted by paw2012 View Post
now what? this it what i get after rebooting. trying to upgrade to 17.
dropping to debug shell
Random???
 
Old 07-03-2012, 08:39 PM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by dth4h View Post
What does your line do that is different from towheedm's one?
Mine dropped the unwanted "p"... but pixellany beat me to the punch! Sorry for the duplication. Better too many answers than too few.

Daniel B. Martin
 
Old 07-03-2012, 08:43 PM   #12
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
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.

Anyway, thanks for your reply's and help.
 
Old 07-04-2012, 08:22 AM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Rep: Reputation: 284Reputation: 284Reputation: 284
Uh-oh. Possible flaw.

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
 
Old 07-04-2012, 11:35 AM   #14
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
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.

Thanks for the info though.
 
Old 07-06-2012, 06:12 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by danielbmartin View Post
Uh-oh. Possible flaw.

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
 
2 members found this post helpful.
  


Reply

Tags
grep, regular expression, syntax


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Some question when using regular expression , ask for help! 915086731 Programming 7 08-21-2011 10:37 AM
Regular expression question gauge73 Linux - General 5 10-28-2005 11:33 AM
Regular expression question gauge73 Linux - General 2 10-28-2005 09:32 AM
Regular expression question. groentebroer Programming 2 11-29-2004 09:15 PM
regular expression question Gantrep Linux - Software 2 04-20-2003 04:24 PM


All times are GMT -5. The time now is 02:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration