LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-11-2005, 03:34 PM   #1
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Rep: Reputation: 30
HOw do I write this regular expression


Fruit Price
Banana 0.89
Peach 0.79
Kiwi 1.50

print only the ones ending with vowel using grep and regular expressions. I would be able to figure it out from notes if the lecturer bothered to write them properly. GRRR
 
Old 10-11-2005, 03:38 PM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 10-11-2005, 06:26 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
grep ".*[aeiou] " fruit.txt


Cheers,
Tink
 
Old 10-12-2005, 09:22 AM   #4
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
Why do I need the .* in front . I had exactly the same thing except mine didn't have the .*
 
Old 10-12-2005, 12:31 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
You don't, really. I just like to make clear that I'll be
getting a word, not a single letter. With your sample
the results are the same.


Cheers,
Tink
 
Old 10-12-2005, 03:35 PM   #6
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
I forgot to ask why does the space make a difference? Without the space at the end it prints peach.
 
Old 10-12-2005, 03:41 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Because the space determines whether the vowel is
in the middle or at the end of a word. Only "[aeiou]"
will match ANY LINE with a vowel in it.
".*[aeiou]" will match anything (including nothing!!) followed
by a vowel.
".*[aeiou] " will match the above FOLLOWED by a space
(which happens to determine the end of a word).


Cheers,
Tink
 
Old 10-12-2005, 03:50 PM   #8
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
ah cool.

Next question is on our best friend sed yay!! (being sarcastic) I basically have a script which uses grap to trim out the line on apple so I get apple 1.20. I am REQUIRED to use sed to show just the price. Please explain your answer tinkster sed isn;t particularly easy to use. I think I will appriciate we have PERL!!!!
 
Old 10-12-2005, 05:23 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Errrh... I won't be doing your homework mate.

Which problem are you facing, what does your sed
attempt look like?



Cheers,
Tink
 
Old 10-13-2005, 03:01 PM   #10
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
Done that question now.

backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
Debian-exim:x:102:102::/var/spool/exim4:/bin/false
identd:x:100:65534::/var/run/identd:/bin/false

how come this still displays uid 100 and over and now would i tell grep to work on the uid and not gid?
grep '.*[0-99]' /etc/passwd

DO i have to filter out the colon and replace it with space? Just guessing thought it may not recognise it as a number with colon in front


Last edited by linuxmandrake; 10-13-2005 at 03:04 PM.
 
Old 10-13-2005, 05:53 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by linuxmandrake
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
Debian-exim:x:102:102::/var/spool/exim4:/bin/false
identd:x:100:65534::/var/run/identd:/bin/false

how come this still displays uid 100
grep '.*[0-99]' /etc/passwd
Because the period matches a 1 and the 0 matches a zero, in
fact the regex matches ANYTHING that has at least one digit.

Quote:
and over and now would i tell grep to work on the uid and not gid?
By exploiting the fact that the uid is preced by "x:" and the gid isn't.


Cheers,
Tink
 
Old 10-15-2005, 06:34 AM   #12
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
I swear this aint in my notes. How do I put two expressions together so either of tham matches print out the lines that match. The | doesn't work

Last edited by linuxmandrake; 10-15-2005 at 06:36 AM.
 
Old 10-15-2005, 08:12 AM   #13
simcox1
Member
 
Registered: Mar 2005
Location: UK
Distribution: Slackware
Posts: 794
Blog Entries: 2

Rep: Reputation: 30
I'm sure you'd learn it a lot better if you figured it out yourself. I've just been reading through a book called the 'Linux Cookbook', which covers things like that. Regexp's, it is fairly complicated by the looks of it. I don't know if you've seen that book but it's really good for basics on a whole host of topics.

PS I've even written a review of it if you're interested. Book reviews on this site.

Last edited by simcox1; 10-15-2005 at 08:54 AM.
 
Old 10-15-2005, 09:35 AM   #14
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by linuxmandrake
grep '.*[0-99]' /etc/passwd

DO i have to filter out the colon and replace it with space? Just guessing thought it may not recognise it as a number with colon in front
The square brackets match exactly one character, not a range of integer values. [0-9] will match a single character in the range 0 thru 9, so your pattern is matching zero or more of any character (the ".*") followed by a single character in the range 0 thru 9 or 9 (so the second 9 is redundant). Simple pattern to match your sample text:
Code:
grep 'x:[0-9][0-9]:' /etc/passwd
will match all the 2-digit uids.
 
Old 10-15-2005, 09:37 AM   #15
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
guess what i done it before u posted eddiebaby1023. Wahoooo Am good

Last edited by linuxmandrake; 10-15-2005 at 09:43 AM.
 
  


Reply



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
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:49 PM.

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
Open Source Consulting | Domain Registration