Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-01-2009
Registered User
 

Join Date: Mar 2009
Posts: 4
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
Sponsored Links
  #2 (permalink)  
Old 04-01-2009
pmm pmm is offline
Registered User
 

Join Date: Dec 2008
Posts: 51

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

  #3 (permalink)  
Old 04-01-2009
Registered User
 

Join Date: Mar 2009
Posts: 4
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....

Thanks and regards,
Pravin Goyal
  #4 (permalink)  
Old 04-01-2009
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,133
below perl code should throw some light on you.

input:

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

output:

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

code:

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];
}

Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
using sed or grep to find exact match of text risk_sly UNIX for Dummies Questions & Answers 5 10-23-2008 12:01 AM
List file in Dir and then match aliahsan81 Shell Programming and Scripting 8 09-22-2008 07:05 AM
sed: find match and delete the line above cstovall Shell Programming and Scripting 3 07-02-2008 11:31 PM
Find match in two diff file - local srv and remote server amir07 Shell Programming and Scripting 0 03-07-2008 10:23 AM
combining 2 files with more than one match in second file johnes42 Shell Programming and Scripting 5 12-04-2007 06:58 AM



All times are GMT -4. The time now is 09:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0