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
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:
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:
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