LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   HOw do I write this regular expression (https://www.linuxquestions.org/questions/programming-9/how-do-i-write-this-regular-expression-372007/)

linuxmandrake 10-11-2005 03:34 PM

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

XavierP 10-11-2005 03:38 PM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

Tinkster 10-11-2005 06:26 PM

Code:

grep ".*[aeiou] " fruit.txt


Cheers,
Tink

linuxmandrake 10-12-2005 09:22 AM

Why do I need the .* in front . I had exactly the same thing except mine didn't have the .*

Tinkster 10-12-2005 12:31 PM

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

linuxmandrake 10-12-2005 03:35 PM

I forgot to ask why does the space make a difference? Without the space at the end it prints peach.

Tinkster 10-12-2005 03:41 PM

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

linuxmandrake 10-12-2005 03:50 PM

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!!!!

Tinkster 10-12-2005 05:23 PM

Errrh... I won't be doing your homework mate.

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



Cheers,
Tink

linuxmandrake 10-13-2005 03:01 PM

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


Tinkster 10-13-2005 05:53 PM

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

linuxmandrake 10-15-2005 06:34 AM

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

simcox1 10-15-2005 08:12 AM

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.

eddiebaby1023 10-15-2005 09:35 AM

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.

linuxmandrake 10-15-2005 09:37 AM

guess what i done it before u posted eddiebaby1023. Wahoooo Am good


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