grep question͏͏
I’m currently stuck on a question for my Unix course that requires us to search for any words that have the following characteristics. I was wondering if anyone could assist in crafting such a command, as it’s far more convoluted than anything I’ve done before. The characteristics for a word in the English language are as follows.
It must …
- be exactly 5 characters long
- begin with an upper or lowercase vowel (a, e, i, o, u, or y)
- have a lowercase ‘t’ in the middle position.
- end with a lowercase ‘s’
- The rest of the characters can be any character that occur in English words; i.e, uppercase, lowercase, hyphen, numbers, etc.
So far I’ve gotten something like this.
[aeiouy].t.s
within brackets and such.
All of this must be using grep and use /usr/share/dict/words
to find what the command reads.
I just need to know what parameters to use. Pretty dang confusing.
The main issue I’m finding is ensuring the first character is uppercase OR lowercase, whilst ensuring the remaining characters can be interchangeable as uppercase or lowercase.
/usr/dict/words
has one word per line.
The main thing you are missing are the anchors to match the beginning and/or end of the word. As words are one per line, you can use ^
and $
which match the start and end of string (line).
grep '^[AEIOUYaeiouy].t.s$' /usr/dict/words
Lots of versions of regular expression tools have extensions to allow word boundaries, e.g.,
grep '<[AEIOUYaeiouy].t.s>' /usr/dict/words
grep "^[aeiouAEIOU].t.sb" filename