msgbartop
Adam Palmer MBCS CITP, Linux, PHP Programmer, MySQL Developer, Embedded Hardware, Security Consultant
Did my blog help you? Please link to me!
  dns test
 
RSS Feed
msgbarbottom

05 Apr 09 Simple text sorting

On the command line we have a number of powerful tools available to us. I’m going to cover some text sorting methods here.

I have a file called ‘testfile’ within this file is the following:

test:~# cat testfile
line1
line3
abcdefg

test
line9

this is a test
test file

test
(more…)

Tags: , , , , , ,

05 Mar 09 BASH Shell Scripting – Sort a string alphabetically

I was asked today how to sort a string alphabetically with BASH

Using perl, you can easily enough use

print (join “”, sort split //,$_)

With bash however, the best option is:

echo “teststring” | grep -o . | sort -n |tr -d ‘\n’; echo
Which returns: eginrssttt

A good way of enumerating each character from a string in general is:

for (( i = 0; i < ${#str[@]}; i++ )); do echo “${str[$i]}”; done

Tags: , , , , , , , , , , , ,