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: awk, cat, grep, sed, sort, text sort, uniq
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: bash, do, done, echo, for, grep, perl, print, shell, shell scripting, sort, string, tr