If I were doing this in perl, I would slurp the entire file in and munge it so that I could sort the original raw lines based on their sequence number. I'm not sure how consistent your file format is, but one perl approach might be:
#!/usr/bin/perl -w
my @data;
# slurp in each line, and tag it by its sequence number
foreach my $line ( <STDIN> )
{
if ($line =~ /sequence=(\S+)/)
{
push @data, { sequence => $1, line => $line };
} else
{
die "unhandled line: $line"; # only if needed
}
}
# sort the lines by their sequence number into @sorted
my @sorted = sort { $a->{sequence} <=> $b->{sequence} } @data;
# generate the final result by extracting the original lines
# from the sorted array and concatenating them
my $result = join("", map { $_->{line} } @sorted);
# output the sorted result
print $result;
I tried this on your example above and it did the trick. You might massage the die
line if you could have "garbage" lines in the input that the script can safely ignore.
Also, if you need to switch between ascending and descending sequence order, you can swap $a
and $b
in this line:
my @sorted = sort { $a->{sequence} <=> $b->{sequence} } @data;
If the sequence numbers aren't purely numeric, or you want to compare them as strings, change the <=>
operator for cmp
:
my @sorted = sort { $a->{sequence} cmp $b->{sequence} } @data;