LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 03-10-2008, 12:18 PM   #1
alenD
Member
 
Registered: Jul 2005
Posts: 69

Rep: Reputation: 15
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
 
Old 03-10-2008, 12:29 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
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...

Last edited by acid_kewpie; 03-10-2008 at 12:31 PM.
 
Old 03-10-2008, 01:05 PM   #3
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
IIRC, a bare + is not special to sed, you need to escape it.
 
Old 03-10-2008, 01:21 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
If you want sed to accept + you need to use the -r switch.


Cheers,
Tink
 
Old 03-10-2008, 02:23 PM   #5
alenD
Member
 
Registered: Jul 2005
Posts: 69

Original Poster
Rep: Reputation: 15
running the command above = simply running echo. Am i missing something?
 
Old 03-10-2008, 02:27 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 03-10-2008, 02:33 PM   #7
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
*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.
 
Old 03-10-2008, 02:42 PM   #8
alenD
Member
 
Registered: Jul 2005
Posts: 69

Original Poster
Rep: Reputation: 15
no, i tried exactly the same line
prints 'some text (123, 456)'
 
Old 03-10-2008, 02:43 PM   #9
alenD
Member
 
Registered: Jul 2005
Posts: 69

Original Poster
Rep: Reputation: 15
ok ,fixed it.... space missing.. thanx a lot
 
Old 03-10-2008, 03:03 PM   #10
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
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.
 
Old 03-10-2008, 03:31 PM   #11
alenD
Member
 
Registered: Jul 2005
Posts: 69

Original Poster
Rep: Reputation: 15
cool.. thanx!
 
  


Reply


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
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 04:16 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 02:47 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 09:34 AM


All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
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