tags:

views:

250

answers:

6

I have a text file 'samp' that I want to grep ONLY lines that start and end with uppercase vowels.

Using "grep ^[AEIOU] samp" works.
Using "grep [AEIOU]$ samp" works as well.

But when trying to combine them to "grep ^[AEIOU]$ samp" it returns nothing.

3 things:

  • Yes, I have lines that start and end with uppercase vowels in samp.
  • Yes, I tried every combination of quotes I could think of. Nothing still.
  • Yes, I'm new to unix.

Any help would be appreciated.

+1  A: 

Your pattern allows for exactly one vowel on the line. Try:

grep ^[AEIOU].*[AEIOU]$

Note that this will not now match lines with a single vowel, if you need that too then we need to get a bit cleverer and use some "or"s.

djna
only problem with this is that it won't match a line with exactly 1 captical vowel on it.
tster
+1  A: 

Your "combined" example is looking for lines that consist of a SINGLE uppercase vowel on a line! What you wanted is:

grep '^[AIOUE].*[AIOUE]$' samp
scrible
@scrible As @tster points out on another answer, your regex won't match a line with exactly 1 capital vowel on it.
Asaph
+8  A: 

What you are giving is a grep for lines that are exactly 1 capital vowel.

Try this:

 <cmd> | grep '[AEIOU]$' | grep '^[AEIOU]'

I'm sure it can be done using one grep, but I don't know the unix grep very well. If the regex is like perl it would be

 <cmd> | grep '^[AEIOU](.*[AEIOU])?$'
tster
I added quotes so that bash wouldn't try to process the brackets and $ signs. Also prefer the first since grep is quite often faster for simple REs (even if two processes are running).
paxdiablo
Agreed, *nix specializes in piping output through programs like that anyways.
tster
A: 

In discussing grep, the 1st edition of Mastering Regular Expressions by Jeffrey Friedl has a "Superficial Survey of a Few Common Programs' Flavor" in Table 6-1 on page 182.

He says "even something as simple as grep varies widely"

The regular expressions in grep are NOT the same as Perl.

Even egrep with extended regular expressions is not the same, but I find it easier to use.

pavium
A: 

This will match a line that consists only of any number of capital vowels (this includes zero, so it matches blank lines):

grep '^[AEIOU]*$'

This matches lines that consist only of one or more capital vowels (does not match blank lines):

grep -E '^[AEIOU]+$'

Either of these will match a line that consists of only one capital vowel.

Dennis Williamson
A: 

Using egrep you could do something like:

echo $'hello\nworld' | egrep '^h|d$'