I have a list of books inside a directory which I'm reading from and I want to list all words from the books that end with the lower and uppercase of a specific letter.

There's probably a way of doing it using cut or tr but I just can't think about it. Is there a way to use globs inside the pipe for it?

I'm not allowed to use grep, sed, awk, or perl.

Edit: The closest I got was:

cat * | tr '[:punct:]' ' '| tr ' ' '\n' | tr -s '\n'| tr '[:upper:]' '[:lower:]' | rev | sort

With this I can have the list of all words, one in each line. We don't really need to get rid of duplicates. Now I would just need to filter the words that have q in the beginning.


You could do something like:

< book.txt \
  tr -sc '[:alnum:]_-' '[\n*]' |
  tr -d - |
  rev |
  cut -c1 |
  tr -cd eE |
  fold -w1 |
  sort |
  uniq -c

To count the es and Es at the end of words (here defined as sequences of alphanumericals or underscores or hyphens), but note some of the limitations:

  • with many implementations including the GNU ones, that only works for single-byte characters.
  • rev, though common is not a standard command.
  • USA would be one word and U.S.A. three words.
  • even with implementations that work correctly with characters, it would count 2 es in Stéphane if the é was written in its decomposed form (e followed by U+0301 the combining acute accent).
  • it doesn't handle hyphenation.
  • it would count one e in 1.02e+23 or 0xffe.5fp-4...

If you were restricted to the POSIX shell and utilities, you could also use ed:

ed -s book.txt << 'EOF' | sort | uniq -c

Or with sh:

l=0 u=0
< book.txt \
  tr -sc '[:alnum:]_-' '[\n*]' | {
    while IFS= read -r word; do
      case $word in
        (*e) l=$((l + 1));;
        (*E) u=$((u + 1));;
    printf '%s\t%s\n' "$l" e "$u" E
  • Thank you for this! Why did you use fold for? I edited my main post with my partial solution. There's no problem considering U.S.A as three words. – Tess Jan 23 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.