Sort by specific order?

Hello all

I was wondering if someone has an idea how to sort by a specific order, let's say by a specific alphabet containing only 4 letters like (d,s,a,p) instead of (a,b,c....z) ??

Cheers!

Could you give input/output examples?

Sure, here's an example.
A file containing the following ascii text:

axxx whatever xxxx ...
pxxx whatever xxxx ...
daxxx whatever xxxx ...
sxxx whatever xxxx ...
dbxxx whatever xxxx ...

should be sorted as:

daxxx whatever xxxx ...
dbxxx whatever xxxx ...
sxxx whatever xxxx ...
axxx whatever xxxx ...
pxxx whatever xxxx ...

Only first char is important to define order (order should be d,s,a,p) - which means, fist char will always be one of this letters (d,s,a,p)
After first char normal alphanumeric sort is fine (1,2...9,a,b,c,d...z)

You can give this a try (not tested):

awk '/^d/{print 1 $0} /^s/{print 2 $0}/^a/{print 3 $0}/^p/{print 4 $0}' file | sort | awk '{print substr($0,2)}'

you can try using something like below:
source file:
88

$ cat ishan1
axxx whatever xxxx ...
pxxx whatever xxxx ...
daxxx whatever xxxx ...
sxxx whatever xxxx ...
dbxxx whatever xxxx ...
axxx whatever xxxx ...
pxxx whatever xxxx ...
daxxx whatever xxxx ...
sxxx whatever xxxx ...
dbxxx whatever xxxx ...
axxx whatever xxxx ...
pxxx whatever xxxx ...
daxxx whatever xxxx ...
sxxx whatever xxxx ...
dbxxx whatever xxxx ...

you can now have a script like:
"ishan2.ksh" 5 lines, 102 characters

#!/usr/bin/ksh
cat ishan1 | grep ^d
cat ishan1 | grep ^s
cat ishan1 | grep ^a
cat ishan1 | grep ^p

the output will be like:

daxxx whatever xxxx ...
dbxxx whatever xxxx ...
daxxx whatever xxxx ...
dbxxx whatever xxxx ...
daxxx whatever xxxx ...
dbxxx whatever xxxx ...
sxxx whatever xxxx ...
sxxx whatever xxxx ...
sxxx whatever xxxx ...
axxx whatever xxxx ...
axxx whatever xxxx ...
axxx whatever xxxx ...
pxxx whatever xxxx ...
pxxx whatever xxxx ...
pxxx whatever xxxx ...

awk thingie:

awk '{f=substr($1,1,1); A[f]=A[f] $0 RS} END{ s=A["d"] A["s"] A["a"] A["p"]; print s}' ORS= file

--
Some awks can do this:

awk '{A[$1]=A[$1] $0 RS} END{ n=split(order,O); for(i=1; i<=n; i++) print A[O]}' order=dsap FS= ORS= file

---
elaborating on Ishan's grep example:

for i in d s a p; do
  grep "^$i" infile
done > outfile

--
sorting of the characters after the first character:

sed 's/./& /' file | sort -k2 |
awk '{f=$1; sub(FS,x); A[f]=A[f] $0 RS} END{ s=A["d"] A["s"] A["a"] A["p"]; print s}' ORS=  
1 Like
perl -e'BEGIN {
  %h = map { $_, ++$c } split //, shift
  }
  print sort { $h{substr $a, 0, 1} <=> $h{substr $b, 0, 1} } <>  
  ' "dsap" infile
1 Like