Home > Software engineering >  Sort each character of a txt file in linux terminal
Sort each character of a txt file in linux terminal

Time:12-12

(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:]

CodePudding user response:

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
  • Related