Wednesday, April 6, 2011

Sort characters in a string in UNIX

I would like to sort the characters in a string.

e.g.

echo cba | sort-command
abc

Is there a command that will allow me to do this or will I have to write an awk script to iterate over the string and sort it?

Thanks

From stackoverflow
  • This is cheating (because it uses Perl), but works. :-P

    echo cba | perl -pe 'chomp; $_ = join "", sort split //'
    
    Mike Nelson : or Ruby ;) `echo cba | ruby -e 'puts ARGF.read.split(%r{\s*}).sort.join'`
  • echo cba | grep -o . | sort |tr -d "\n"
    

0 comments:

Post a Comment