2

Using bash I would like to sort a list of text strings in accordance with a first and a second order criteria:

  1. Number of characters in the text string; string with fewest characters first
  2. In order of the Danish alphabet which is the same as the English except for the letters æ, ø, å in the end (after z)

Example:

I would like this list:

aabb
ccc
aaaa
ddd
dgg
øøøø
aa
cc
ab

To be sorted into this:

aa
ab
cc
ccc
ddd
dgg
aaaa
aabb
øøøø

How can that be achieved?

MajorBriggs
  • 556
  • 4
  • 18
  • possible duplicate of [How to sort an array in BASH](http://stackoverflow.com/questions/7442417/how-to-sort-an-array-in-bash) –  Oct 21 '14 at 13:54
  • @MrCoder: No; this question has significant additional requirements. – tripleee Oct 21 '14 at 14:16

2 Answers2

1

With bash, sort and cut:

while read -r l; do echo "${#l} $l"; done < filename | sort -n | cut -d " " -f 2-

Output:

aa
ab
cc
ccc
ddd
dgg
aaaa
aabb
øøøø
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • You need to use `read -r` and echo `"$l"` in double quotes, otherwise you will wreck input containing backslashes, runs of whitespace, glob characters, etc. – tripleee Oct 21 '14 at 14:13
  • 1
    Without the appropriate locale, it is unlikely that words containing æ ø å will be sorted correctly. – tripleee Oct 21 '14 at 14:17
  • 1
    In the very rare event that leading or trailing whitespace is important, `while IFS= read -r l` or `while read -r;` (when using the default variable `REPLY`, word-splitting is not applied to the value). – chepner Oct 21 '14 at 16:16
  • 1
    This sorting gets right with `LANG=da_DK.UTF-8` in `/etc/default/locale` (using Debian). – MajorBriggs Oct 23 '14 at 20:58
1

You need a Schwartzian transform.

LC_SORT=da_DK perl -lpe 'print (len(), "\t")' input |
sort -k1n -k2,2 |
cut -f2- > output
tripleee
  • 175,061
  • 34
  • 275
  • 318