C really isn't the tool for this job.
Perl on the other hand is.
Code:
#!/bin/perl -w
use strict;
# A hash containing all the composite lines
my %lines = ();
while ( <> ) { # for every line of every file
chomp;s/\r//; # remove any \n or \r
if ( /^([0-9]+)(.*)/ ) { # separate the first number($1) and the rest line($2)
if ( ! exists $lines{$1} ) {
$lines{$1} = $1; # start each new line with the key
}
$lines{$1} .= $2; # append the rest of the line
}
}
# print all the lines when we've done reading files.
for my $i ( sort keys %lines ) {
print "$lines{$i}\n";
}
Which produces these kinds of results
Code:
$ perl foo.pl foo.txt
787500000, -8.07900e+01, -2.14748e+06, -2.14748e+06
787562500, -8.09020e+01, -2.14748e+06, -2.14748e+06
787625000, -8.04260e+01, -2.14748e+06, -2.14748e+06
787687500, -8.05870e+01, -2.14748e+06, -2.14748e+06
787750000, -8.04020e+01, -2.14748e+06, -2.14748e+06
$ perl foo.pl foo.txt foo.txt
787500000, -8.07900e+01, -2.14748e+06, -2.14748e+06, -8.07900e+01, -2.14748e+06, -2.14748e+06
787562500, -8.09020e+01, -2.14748e+06, -2.14748e+06, -8.09020e+01, -2.14748e+06, -2.14748e+06
787625000, -8.04260e+01, -2.14748e+06, -2.14748e+06, -8.04260e+01, -2.14748e+06, -2.14748e+06
787687500, -8.05870e+01, -2.14748e+06, -2.14748e+06, -8.05870e+01, -2.14748e+06, -2.14748e+06
787750000, -8.04020e+01, -2.14748e+06, -2.14748e+06, -8.04020e+01, -2.14748e+06, -2.14748e+06