Why isn't this grep command coming back with nothing? (Bash)

I have a text file 'samp' which I want to grep ONLY lines that start and end with vowel caps.

Works " grep ^[AEIOU] samp

".
Using " grep [AEIOU]$ samp

" also works.

But when trying to combine them with " grep ^[AEIOU]$ samp

" it returns nothing.

3 things:

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


source to share


6 answers


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

Try the following:

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

      



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

 <cmd> | grep '^[AEIOU](.*[AEIOU])?$'

      

+8


source


Your pattern allows for exactly one vowel per line. Try:

grep ^[AEIOU].*[AEIOU]$ 

      



Note that now this will not match single vowel strings, if you need it then we need to digest a little and use some "or".

+1


source


Your "combined" example looks for strings that consist of a single uppercase vowel on a line! You wanted:

grep '^[AIOUE].*[AIOUE]$' samp

      

+1


source


When discussing grep 1st edition "Mastering Regular Expressions" by Jeffrey Friedl, has a "Cursory Taste Assessment of Several Common Programs" in Table 6-1 on page 182.

He says, "Even something as simple as grep varies widely."

Regular expressions in grep are NOT the same as Perl.

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

0


source


This will match a string consisting of only any number of vowel vowels (this includes zero, so it matches empty strings):

grep '^[AEIOU]*$'

      

This matches lines with only one or more main vowels (does not match blank lines):

grep -E '^[AEIOU]+$'

      

Any of these will match a string with only one vowel.

0


source


Using egrep you can do something like:

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

      

0


source







All Articles