Reggie Kapros Reggie Kapros - 6 months ago 16
Linux Question

How to print words that contain only letters?

If I'm given a file how can I print the words that contain only the characters A-Za-z (so only letters) in alphabetical order? Should I use awk?Could someone help me with suggestions/example?

So if I have the file file1.txt
and it looks like this:

ahk4 ble ach cop v59l k9l
h6j alo

The ouptut will look like this:
ach alo ble cop

Answer

To get the words contain alphabet only:

$ tr -cs '[:alnum:]' '[\n*]' <file | grep -E '^[[:alpha:]]+$'
ble
ach
cop
alo

To get your desired output:

$ tr -cs '[:alnum:]' '[\n*]' <file |
  grep -E '^[[:alpha:]]+$'         | 
  sort                             |
  paste -sd ' ' -