0

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

  1. be exactly 5 characters long
  2. begin with an upper or lowercase vowel (a, e, i, o, u, or y)
  3. have a lowercase 't' in the middle position.
  4. end with a lowercase 's'
  5. 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.

5
  • 1
    Why not just use [aeiouyAEIOUY]? Dec 8, 2021 at 19:44
  • 1
    Note that . will also match on things like space or <, ? which usually don't occur in English words Dec 8, 2021 at 19:45
  • That works as well, but for whatever reason I'm also having trouble ensuring it stays as only 5 characters. I've done grep "^[aeiouyAEIOUY].t.s" and it ends up giving me words like Estes's or "Abbott's" which it shouldn't be.
    – Vordt
    Dec 8, 2021 at 19:52
  • Use grep -x '[aeiouyAEIOUY].t.s' or use both the ^ (matches at start of subject) and $ (end of subject) anchors. Dec 8, 2021 at 20:06
  • I am a little confused by the question: "ensure that it is any case" or "allow any case"? Dec 8, 2021 at 20:51

2 Answers 2

2

/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
8
  • That worked! Thank you so much! I knew I was missing something like that, I'd been using ^ to start a line but hadn't used $ and ended up getting words that were too big or too small.
    – Vordt
    Dec 8, 2021 at 19:53
  • 1
    "too big" I can understand, "too small" puzzles me.
    – icarus
    Dec 8, 2021 at 19:55
  • I'd forgot to add brackets on that specific instance. I was trying everything I could think of.
    – Vordt
    Dec 8, 2021 at 19:57
  • Using -w would be more portable than using \<, >, however note that there is a word boundary between - and a in minus-altus for instance (IOW, the definition of word character is not the same between grep's -w/\</\> and the OP's). Dec 8, 2021 at 20:08
  • 1
    You have to practice this yourself. There are good regex tools out there. Including some on-line ones. Play with them, practice, learn. Dec 8, 2021 at 20:52
0
 grep "^[aeiouAEIOU].t.s\b" filename
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .