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. |
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]* Code:
echo abcp21de32a | sed 's,.*\(p[0-9]*\).*,\1,' |
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.
|
Ok, that makes sense. Thanks again.
|
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/' |
Quote:
Code:
sed -r 's/.*p([0-9]*).*/\1/' < $InFile |
what do I do
now what? this it what i get after rebooting. trying to upgrade to 17.
dropping to debug shell |
Quote:
Please start a new thread---and be sure to tell us what you are doing----by itself, the above makes no sense. |
Quote:
Quote:
Quote:
|
Quote:
Daniel B. Martin |
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. |
Uh-oh. Possible flaw.
I expanded the test input file to these three lines: Code:
abcp1de Daniel B. Martin |
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. |
Quote:
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/' |
All times are GMT -5. The time now is 04:02 PM. |