LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple pattern match with awk, sed (https://www.linuxquestions.org/questions/linux-newbie-8/simple-pattern-match-with-awk-sed-626995/)

alenD 03-10-2008 11:18 AM

simple pattern match with awk, sed
 
I have a line with the following pattern:

some text (x, y)

where x y are the numbers
I am looking for a simple awk or sed or bash script that would print out just x and y

thanks

acid_kewpie 03-10-2008 11:29 AM

depends how strict the formatting is but this should work...

$ echo "some text (123, 456)" | sed -e "s/^.*(\([0-9]*\),\ \([0-9]*\)).*$/\1 \2/"
123 456

Can anyone tell *me* though, why it doesn't work if any of the *'s are replaced with +'s? * = 0 or more, + = 1 or more, so i'm clearly missing something, and have done for a long long time...

ararus 03-10-2008 12:05 PM

IIRC, a bare + is not special to sed, you need to escape it.

Tinkster 03-10-2008 12:21 PM

If you want sed to accept + you need to use the -r switch.


Cheers,
Tink

alenD 03-10-2008 01:23 PM

running the command above = simply running echo. Am i missing something?

Tinkster 03-10-2008 01:27 PM

Code:

echo "some text (123, 456)" | sed -e "s/^.*(\([0-9]*\),\ \([0-9]*\)).*$/\1 \2/"
Apparently... it works fine here. What does your exact string
actually look like?


Cheers,
Tink

ararus 03-10-2008 01:33 PM

*blink*

-r extended regex

Where the frell did that come from? I never noticed that before. I'm using Slack 10.2 here (too lazy to upgrade) and I just noticed I have it here, probably been there for years. Gah, what sneaks past my myopia, gets shot down by my attention deficit disorder.

alenD 03-10-2008 01:42 PM

no, i tried exactly the same line
prints 'some text (123, 456)'

alenD 03-10-2008 01:43 PM

ok ,fixed it.... space missing.. thanx a lot

ararus 03-10-2008 02:03 PM

Btw, depending on your input, you might want to use no printing mode, so lines that don't match will be ignored. Give sed the -n option and whack a p after the final delimiter,

Code:

whatever... | sed -n 's/...../\1 \2/p'
If all your input is in the format you specified it shouldn't matter, but if it has extraneous stuff you're not interested in, it will be printed out by default without the -n option.

This also makes it easy to see if your pattern is working, because sed gives you no output at all if it doesn't match, rather than simply echoing the input.

alenD 03-10-2008 02:31 PM

cool.. thanx!


All times are GMT -5. The time now is 06:34 PM.