StackerCen
StackerCen

Reputation: 85

Sort each character of a txt file in linux terminal

(using linux commands in eg Ubuntu)

So you have a certain text file file1.txt for example.

I want to sort this file in alphabetical order for each character.

This so I can then use uniq or something on it so I can obtain a sting with all the unique characters that occur in my file

The file would read as follows: The man came home.

So the output would be: aaceeehhmmmnot

What I have so far is just: cat file1.txt | tr A-Z a-z | tr -dc [:alpha:]

Upvotes: 0

Views: 173

Answers (1)

Hamid Rouhani
Hamid Rouhani

Reputation: 2459

You can split characters to lines, sort them and finally join all lines to get the result:

$ cat file1.txt | tr A-Z a-z | tr -dc '[:alpha:]' | grep -o . | sort | tr -d "\n"
aaceeehhmmmnot

Upvotes: 2

Related Questions