LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   question about using grep (https://www.linuxquestions.org/questions/linux-newbie-8/question-about-using-grep-4175640143/)

curiouspinguin 10-11-2018 01:19 AM

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.

Turbocapitalist 10-11-2018 01:50 AM

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.

pan64 10-11-2018 01:56 AM

Quote:

Originally Posted by curiouspinguin (Post 5913479)
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...

l0f4r0 10-11-2018 03:20 AM

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).

nodir 10-11-2018 06:48 AM

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.

BW-userx 10-11-2018 07:26 AM

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.

curiouspinguin 10-11-2018 10:57 PM

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

Mike25 10-11-2018 11:18 PM

Quote:

Originally Posted by curiouspinguin (Post 5913845)
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)

l0f4r0 10-12-2018 03:09 AM

Quote:

Originally Posted by curiouspinguin (Post 5913845)
[...]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 (Post 5913852)
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) ;)

pan64 10-12-2018 04:10 AM

probably you want to check character classes like [:alpha:] and similar.
https://www.gnu.org/software/grep/ma...pressions.html

curiouspinguin 10-12-2018 06:57 AM

great link
 
Quote:

Originally Posted by pan64 (Post 5913915)
probably you want to check character classes like [:alpha:] and similar.
https://www.gnu.org/software/grep/ma...pressions.html

Great link!!! Much appreciated.


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