script help

I have some flat files with time stamp,and some not with time stamp ,I need the files to list with time stamp and non time stamp with sequencial manner.I wrote the shell script to list the files with time stamp ,but non time stamp when use another for loop,its giving more file list.Any advise it should be appereciated.

Files
=====

file120081218-153431-630.txt
file220081318-153431-630.txt
file320081218-153431-630.txt
file420081218-153431-630.txt
file5.txt
file6.txt



for a in `ls -lrt |awk '{print $9}'|awk '{print substr($0,(index($0,"-"))-1),-8}'|sort -u`
do 
echo $a
for a1 in `ls lrt *$a*|awk '{print $9}'`
do
echo $a1
done
done


20081218
file120081218-153431-630.txt
file320081218-153431-630.txt
file420081218-153431-630.txt

20081318
file220081318-153431-630.txt

expecting o/p

20081218
file120081218-153431-630.txt
file320081218-153431-630.txt
file420081218-153431-630.txt

20081318
file220081318-153431-630.txt

file5.txt
file6.txt

Thanks in advance
Akil

Maybe this ?

$ 
$ ls -1
file120081218-153431-630.txt
file220081318-153431-630.txt
file320081218-153431-630.txt
file420081218-153431-630.txt
file5.txt
file6.txt
$ 
$ 
$ ls -1 | sort -k1.6,1.28 | awk '{ymd=substr($0,6,8); print ymd != prevymd ? (ymd != ".txt" ? "\n"ymd : "")"\n" :"",$0; prevymd=ymd}'

20081218
 file120081218-153431-630.txt
 file320081218-153431-630.txt
 file420081218-153431-630.txt

20081318
 file220081318-153431-630.txt

 file5.txt
 file6.txt
$ 
$

tyler_durden

Hi ,
I am getting the below error meeage when tring
ls -1 | sort -k1.6,1.28 | awk '{ymd=substr($0,6,8); print ymd != prevymd ? (ymd != ".txt" ? "\n"ymd : "")"\n" :"",$0; prevymd=ymd}'

syntax error The source line is 1.
The error context is
{ymd=substr($0,6,8); print ymd >>> != <<<
awk: The statement cannot be correctly parsed.

Thanks,
Akil

Need to add paraenthesis...

ls -1 | /bin/sort -k1.6,1.28 | awk '{ymd=substr($0,6,8); print ((ymd != prevymd) ? (ymd != ".txt" ? "\n"ymd : "")"\n" :"",$0); prevymd=ymd}'

Output

 file5.txt
 file6.txt

20081218
 file120081218-153431-630.txt
 file320081218-153431-630.txt
 file420081218-153431-630.txt

20081318
 file220081318-153431-630.txt

how about below perl script?

use strict;
my(@arr,%hash);
while(<DATA>){
	chomp;
	if (/^[^0-9]+([0-9]+)([0-9]{4}[0-9]{2}[0-9]{2})/){
		$hash{$2}.=$_."\n";
	}
	elsif(/^[^0-9]+([0-9]+)/){
		push @arr, [$1,$_];
	}
}
foreach my $key(sort {$a<=>$b} keys %hash){
	print $key,"\n";
	print $hash{$key};
	print "\n";
}
print join "\n", map {$_->[1]} sort {$a->[0]<=>$b->[0]} @arr;
__DATA__
file10.txt
file320081218-153431-630.txt
file120081218-153431-630.txt
file220081318-153431-630.txt
file420081218-153431-630.txt
file5.txt
file6.txt