Sort based on positions in flat file

Hello,

For example:

12........6789101112..............20212223242526..................50  ( Positions)
LName       FName                     DOB                                    (Lastname starts from 1 to 6 , FName from 8 to 15 and date of birth from 21 to29)
 
CURTIS     KENNETH                   19770607
WALKER    CHARLES                   19380105
COHAN      TREVA                      19510506

I want to sort above records based on First name, lastname and Date of Birth using Unix Script.
I have stated specific positions for firstname, last name and date of birth in the above.

Output look like

COHAN      TREVA                      19510506
CURTIS     KENNETH                   19770607
WALKER    CHARLES                   19380105

How can we achieve this?

bash-3.2$ cat x
12345678910121314151617181920 (Positions)
SAchin      Tendular           100
Virat         Kolhi                 99
AAA          BBBB                00
BBB           XXX                 10
bash-3.2$ 
bash-3.2$ sort -nr -k 3 x
SAchin      Tendular           100
Virat         Kolhi                 99
BBB           XXX                 10
AAA          BBBB                00
12345678910121314151617181920 (Positions)

I don't understand what

means, but I'm guessing that reverse numeric order of the values in the 3rd column (as provided by MR.bean's suggestion) is not what you want.

Please explain more clearly the fields you want to be used as sort keys and show us the output you want to be produced.

Please use code tags as required by forum rules!

You seem to have edited your initial post#1 after the comments of Don Cragun. Still, due to missing code tags, your input sample does NOT obey the specification (field positions) that you have given. Plus, the desired output does NOT conform to your specs, either - it is sorted for last name, not first name as desired. Try this (based on guesses and input modified by me to be reasonable) and come back with results/comments:

sort -k1.8,1.15 -k1.1,1.6 -k1.21,1.29n file

Now that I have put CODE tags back into the modified 1st message in this thread, we can see that the DOB and FName fields are not in the columns specified.

Ignoring header lines that might or might not be part of the input file, sorting by FName and LName can be done with the command:

sort -k 2,2 -k1,1 file

If you want to sort by LName and FName (as shown in the sample output you said you want), that can be done with the command:

sort file

python

def leo(x):
 return(x[0],x[1],int(x[2]))
d=[]
with open("a.txt") as file:
 for line in file:
  line=line.replace("\n","")
  items = line.split(" ")
  d.append(items)
for i in sorted(d,key=leo):
 print(i)

perl

my @result;
while(<DATA>){
	my @arr=split(" ",$_);
	push @result,\@arr;
}
for my $val (sort {$a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} @result){
	print join " ", @$val;
	print "\n";
}
__DATA__
c d 10
a b 20
d c 10
c a 10
c d 20
b b 100
d a 13
d c 8