Thursday, May 16, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  16] [ 0]  / answers: 1 / hits: 6195  / 3 Years ago, sun, june 20, 2021, 5:11:06

How can I turn this:



Johnny's penguin, (Tuxie), likes the following foods: French fries, and beef.


To this:



 abcdefghiklnoprstuwFJT',():.


(These are the total characters used in the input)



Please note that the small case characters "jmqvz" were not in the input sentence, therefore, not outputted.



The order is not important whatsoever, but lower case, then upper case, then special characters will be preferred.



I am certain I will need sed/awk/etc. for this, but I have not found anything similar after extensive searching.


More From » bash

 Answers
6

You can use a combination of sed and sort:



$ echo "Johnny's penguin, (Tuxie), likes the following foods: French fries, and beef." | 
> sed 's/./&
/g' | LC_COLLATE=C sort -u | tr -d '
'
'(),.:FJTabcdefghiklnoprstuwxy


sort does lexicographic sorting, so see man 7 ascii to see how the characters will order up.



Explanation:




  • sed 's/./&
    /g'
    - add a newline after every character, since sort (usually) does line-by-line sorting

  • LC_COLLATE=C sets the collation style to C (see What does “LC_ALL=C” do?)

  • sort -u: sorts the input and prints only the unique entries

  • tr -d '
    '
    deletes all the extra new lines.



If you want to keep only visible characters:



$ echo "Johnny's penguin, (Tuxie), likes the following foods: French fries, and beef." | 
> tr -cd '[[:graph:]]' | sed 's/./&
/g' | LC_COLLATE=C sort -u | tr -d '
'



  • tr -cd '[[:graph:]]' deletes everything except visible characters.


[#20682] Monday, June 21, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
atereress

Total Points: 79
Total Questions: 106
Total Answers: 119

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
;