How to find first match and last match in a file

Hi All,
I have a below file:

==================
02:53 pravin-root
02:53 pravin-root
03:05 pravin-root
02:55 pravin1-root
02:59 pravin1-root

How do I find the first and last value of column 1. For example, how do I find 02:53 is the first time stamp and 03:05 is the last time stamp for user "pravin-root"? The no. of users can be large.

I am trying a few options meanwhile... just posted here if someone already knows how to do it...

Thanks and regards,
Pravin Goyal

awk ' { if ( F[$2] == "" ) F[$2]=$1; if ( $1 > L[$2] ) L[$2]=$1 } END { for ( i in F ) print i " First= " F " Last= " L } '

Hi All,
Thanks for your replies... I solved my problem...

cut -f1 -d\ temp5_log > temp6_log
t1=`head -n1 temp6_log`
t2=`tail -n1 temp6_log`
./calc_time.sh $t1 $t2

I previously wrote a script calc_time.sh to calculate time difference.... :slight_smile:

Thanks and regards,
Pravin Goyal

below perl code should throw some light on you.

input:

1 pravin-root 
3 pravin-root 
3 a
2 pravin-root
2 a
3 pravin1-root
2 pravin1-root
1 a

output:

a 1
a 3
pravin-root 1
pravin-root 3
pravin1-root 2
pravin1-root 3

code:

my %hash;
open my $fh,"<","a.spl";
while(<$fh>){
	chomp;
	my @arr=split;
	push @{$hash{$arr[1]}}, $arr[0];
}
foreach $key (sort keys %hash){
	my @tmp=sort @{$hash{$key}};
	map { print $key," ",$_,"\n" } @tmp[0,$#tmp];
}