0

I am having difficulty finding solutions online to sort an absolute directory path using letters and numbers.

Example is the below in a file called test.txt

 /opt/informix/data/dcdwhdev/rootdbs.2
 /opt/informix/data/dcdwhdev/db1.1
 /opt/informix/data/dcdwhdev/db1.10
 /opt/informix/data/dcdwhdev/db1.11
 /opt/informix/data/dcdwhdev/rootdbs.1
 /opt/informix/data/dcdwhdev/db1.12
 /opt/informix/data/dcdwhdev/db1.19
 /opt/informix/data/dcdwhdev/db1.2
 /opt/informix/data/dcdwhdev/db1.21
 /opt/informix/data/dcdwhdev/db1.22
 /opt/informix/data/dcdwhdev/db1.23
 /opt/informix/data/dcdwhdev/db1.3
 /opt/informix/data/dcdwhdev/db1.31
 /opt/informix/data/dcdwhdev/db1.32
 /opt/informix/data/dcdwhdev/db1.33

Below is what I want to achieve:

/opt/informix/data/dcdwhdev/db1.1
/opt/informix/data/dcdwhdev/db1.2
/opt/informix/data/dcdwhdev/db1.3
/opt/informix/data/dcdwhdev/db1.10
/opt/informix/data/dcdwhdev/db1.11
/opt/informix/data/dcdwhdev/db1.12
/opt/informix/data/dcdwhdev/db1.19
/opt/informix/data/dcdwhdev/db1.21
/opt/informix/data/dcdwhdev/db1.22
/opt/informix/data/dcdwhdev/db1.23
/opt/informix/data/dcdwhdev/db1.31
/opt/informix/data/dcdwhdev/db1.32
/opt/informix/data/dcdwhdev/db1.33
/opt/informix/data/dcdwhdev/rootdbs.1
/opt/informix/data/dcdwhdev/rootdbs.2

2 Answers 2

4

If you have GNU sort (might be called gsort on your system, or located in a /opt/gnu/bin directory, or from the coreutils RPM at IBM.com):

sort -V < test.txt

If zsh is available (RPM package at IBM.com):

print -rC1 -- /(Nne['reply=(${(f)"$(<test.txt)"})'])

Or listing the files in that directory directly:

print -rC1 /opt/informix/data/dcdwhdev/*(Nn)

If not, you could use perl for instance as:

perl -e '
  print $_->[1] for 
    sort {$a->[0] cmp $b->[0]}
      map {[s/\d+/sprintf "%06d", $&/ger, $_]} <STDIN>' < test.txt

Where we sort the list by comparing the lines after all the sequences of decimal digits have been zero-padded to 6 digits.

If the lines in the file are always shaped like that and you only need to compare the part before the one and only . lexically, and the part after it numerically, then, you can just do:

sort -t. -k1,1 -k2,2n < test.txt
0

My process is to break the name into 2 and then sort and bring back together

for fp in `cat test.txt | awk -F\. '{print $1}' | sort | uniq`; do grep $fp test.txt | sed 's/\./\ /g' | sort -nk2 | sed 's/\ /\./g' ;done
./opt/informix/data/dcdwhdev/db1.1
./opt/informix/data/dcdwhdev/db1.2
./opt/informix/data/dcdwhdev/db1.3
./opt/informix/data/dcdwhdev/db1.10
./opt/informix/data/dcdwhdev/db1.11
./opt/informix/data/dcdwhdev/db1.12
./opt/informix/data/dcdwhdev/db1.19
./opt/informix/data/dcdwhdev/db1.21
./opt/informix/data/dcdwhdev/db1.22
./opt/informix/data/dcdwhdev/db1.23
./opt/informix/data/dcdwhdev/db1.31
./opt/informix/data/dcdwhdev/db1.32
./opt/informix/data/dcdwhdev/db1.33
./opt/informix/data/dcdwhdev/rootdbs.1
./opt/informix/data/dcdwhdev/rootdbs.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.