LinuxQuestions.org
Visit Jeremy's Blog.
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 10-11-2018, 02:19 AM   #1
curiouspinguin
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Rep: Reputation: Disabled
question about using grep


hello and thank you in advance for your help. I'm trying to create a grep command for a study exercise that requires me to create a grep command that will match names within a file that and with a vowel, but start with consonants. so far i came up with this: $grep '^[A-Z][AEIOU' ./filename
I do get an output but is not what I'm looking for. I would really appreciate your help.
 
Old 10-11-2018, 02:50 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,372
Blog Entries: 3

Rep: Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184
Welcome. You'll need to review the manual page for grep and turn your attention to the -E or -P options.

Code:
man grep
It is the reference document for grep not a tutorial, however.

Then review your first bracket expression since you ask for a consonant but include all the letters.
 
Old 10-11-2018, 02:56 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
Quote:
Originally Posted by curiouspinguin View Post
I do get an output but is not what I'm looking for. I would really appreciate your help.
Would be nice to give us more details. What did you expect and what has really happened...
 
Old 10-11-2018, 04:20 AM   #4
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 887

Rep: Reputation: 288Reputation: 288Reputation: 288
The pattern should be basic enough not to require options -E and -P, but you can indeed if you want to avoid some escapings
However option -i could be useful.
By the way, "Y" is a (semi-)vowel as well so don't forget it.
Anf finally pay attention to quantifiers, especially "+" (see "Repetition" section of grep man page).
 
Old 10-11-2018, 07:48 AM   #5
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
The most easy approach is probably to first grep for everything that ends with a vowel [aeiou]$ and then grep in a pipe for everything of that that doesn't (--invert-match) start with a vowel ^[aeiou]. Like already mentioned case-insensitive -i might be needed too.
Assuming you don't have to only present the result, but will also have to explain it.
To put it different: i would solve one of the both problems at a time.
If that is sorted, you should easily be able to make it more sexy.
 
Old 10-11-2018, 08:26 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ Win10
Posts: 9,495

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
being this sounding like a standard teacher question to the students, so it's most likely to have been asked before, so I googled it,
Code:
grep -oiw '[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]'
see if that works. the 'w' for matching whole words?
source:
https://unix.stackexchange.com/quest...end-in-a-vowel
a quick test, and it looks like it works.
Code:
$ ls /media/ntfs1 | grep -oiw '[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]' 
RECYCLE
Grande
Grande
Deluxe
Grande
Grande
Deluxe
Grande
The
No
Volume
just make sure you research the why it works, then you can apply that to other niffty grep commands.

Last edited by BW-userx; 10-11-2018 at 08:46 AM.
 
Old 10-11-2018, 11:57 PM   #7
curiouspinguin
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you, everyone!!!
I had come up with this:
grep '^[^aeiou][a-z]*[aeiou]*' ./myfile.txt
but BW-userx command seems to be more specific but I think there's a carat missing and a dollar sign at the end, so this is what I did instead: grep -oiw '^[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]$' and it worked. thank you yall are awesome!!!!

Last edited by curiouspinguin; 10-12-2018 at 12:17 AM. Reason: correction to syntax
 
Old 10-12-2018, 12:18 AM   #8
Mike25
Member
 
Registered: Feb 2003
Location: ON, Canada
Distribution: Fedora & RH3
Posts: 56
Blog Entries: 2

Rep: Reputation: 11
Quote:
Originally Posted by curiouspinguin View Post
Thank you, everyone!!!
I had come up with this:
grep '^[^aeiou][a-z]*[aeiou]*' ./myfile.txt
but BW-userx seems to be more specific and yeah it works too. yall are awesome!!!!
That will work fine as long as the names you are looking for only contain letters. Any numbers, hyphens, underscores, spaces etc, won't match, even if they do start with a consonant and end with a vowel. (just in case)
 
Old 10-12-2018, 04:09 AM   #9
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 887

Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by curiouspinguin View Post
[...]but I think there's a carat missing and a dollar sign at the end, so this is what I did instead: grep -oiw '^[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]$' and it worked.
Considering your new regex, you certainly have only one word per line so you don't need option -w.
Code:
grep -oiw '[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]'
looked more versatile but only you can decide and it's hard to tell because you never provided an example of input...
By the way, please put your code/commands inside [CODE] tags.

Quote:
Originally Posted by Mike25 View Post
That will work fine as long as the names you are looking for only contain letters. Any numbers, hyphens, underscores, spaces etc, won't match, even if they do start with a consonant and end with a vowel. (just in case)
Yes, hence the use of option -w for words delimiter (that doesn't include the numbers & underscores though)
 
Old 10-12-2018, 05:10 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
probably you want to check character classes like [:alpha:] and similar.
https://www.gnu.org/software/grep/ma...pressions.html
 
1 members found this post helpful.
Old 10-12-2018, 07:57 AM   #11
curiouspinguin
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thumbs up great link

Quote:
Originally Posted by pan64 View Post
probably you want to check character classes like [:alpha:] and similar.
https://www.gnu.org/software/grep/ma...pressions.html
Great link!!! Much appreciated.
 
  


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
question about grep tpopono Linux - Newbie 6 02-17-2013 01:40 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
grep question torrent478 Linux - Newbie 2 10-08-2008 07:50 PM
maybe a grep question: me210 Linux - Software 3 01-19-2007 01:13 PM
grep question tokernizer Programming 2 12-20-2005 09:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:29 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration