Extracting Max date for multiple same key


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting Max date for multiple same key
# 1  
Old 09-20-2011
Extracting Max date for multiple same key

Can any one help me to sort and extract the max date row from multiple same key ?

example of input file
kEY DATE(YYY-MM-DD)

Code:
10   2011-08-01
20   2011-09-02
20   2011-10-01
20   2011-08-02
30   2010-01-20
30   2011-01-20


Out put :

Code:
10 2011-08-01co
20 2011-10-01
30 2011-01-20

The out put is extracted based on max date from right column if the left column have same keys key (20/30).

If the left side has no duplicate key then it should pick that record (e.g with key 10)

Thanks
JM
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 09-20-2011 at 04:03 PM.. Reason: code tags, please!
# 2  
Old 09-20-2011
Solution using an awk script.

Code:
[mute@geek ~]$ cat cmp
function cmp_date(a,b) {
  gsub("-", "", a);
  gsub("-", "", b);
  if (a > b) return 1;
  return 0;
}

{ if (cmp_date($2, a[$1])) a[$1] = $2; }
END { for (i in a) print i, a[i]; }
[mute@geek ~]$ awk -f cmp dates
10 2011-08-01
20 2011-10-01
30 2011-01-20

# 3  
Old 09-20-2011
Hi jambesh,

Using 'Perl':
Code:
$ cat jambesh.txt 
10   2011-08-01
20   2011-09-02
20   2011-10-01
20   2011-08-02
30   2010-01-20
30   2011-01-20
$ cat script.pl
use warnings;
use strict;
use Time::Local;

@ARGV == 1 or die qq[Usage: perl $0 input-file\n];

my %key;

while ( <> ) {

        ## Most recent date found and date of current record.
        my ($saved_date, $record_date);

        next if /\A\s*\z/;

        ## Split record fields.
        chomp;
        my ($k, $d) = split;

        ## If key not processed, save its date and go to next record.
        if ( ! exists $key{ $k } ) {
                $key{ $k } = $d;
                next;
        }

        ## Convert dates to utc and compare them. If date or current record is most
        ## recent, save it.
        {
                my ($year,$month,$day) = $key{ $k } =~ m/\A(\d{4})-(\d{2})-(\d{2})\z/;
                $saved_date = timelocal( 0, 0, 0, $day, $month - 1, $year - 1900);
        }

        {
                my ($year,$month,$day) = $d =~ m/\A(\d{4})-(\d{2})-(\d{2})\z/;
                $record_date = timelocal( 0, 0, 0, $day, $month - 1, $year - 1900);
        }

        if ( $record_date - $saved_date > 0 ) {
                $key{ $k } = $d;
        }
}

## Print them.
for ( sort keys %key ) {
        printf "%d\t%s\n", $_, $key{ $_ };
}
$ perl script.pl jambesh.txt 
10      2011-08-01
20      2011-10-01
30      2011-01-20

Regards,
Birei
# 4  
Old 09-20-2011
The order of the keys ($1) will not be preserved/guaranteed.

Code:
awk 'END {
  for (K in k) print K, k[K]
  }
{ $2 > k[$1] && k[$1] = $2 }
  ' infile

# 5  
Old 09-21-2011
If your sort supports the "-s" stable option:
Code:
sort -k1,1 -k2,2r infile |sort -k1,1 -su

# 6  
Old 09-21-2011
Another Perl solution:

Code:
$
$
$ cat f26
10   2011-08-01
20   2011-09-02
20   2011-10-01
20   2011-08-02
30   2010-01-20
30   2011-01-20
$
$
$
$ perl -lane 'if ($F[0] eq $key and $F[1] gt $val) {$val = $F[1]}
              elsif ($F[0] != $key) {print "$key   $val" if defined $key; ($key, $val) = @F}
              END {print "$key   $val"}
             ' f26
10   2011-08-01
20   2011-10-01
30   2011-01-20
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

2. Shell Programming and Scripting

Remove Duplicates on multiple Key Columns and get the Latest Record from Date/Time Column

Hi Experts , we have a CDC file where we need to get the latest record of the Key columns Key Columns will be CDC_FLAG and SRC_PMTN_I and fetch the latest record from the CDC_PRCS_TS Can we do it with a single awk command. Please help.... (3 Replies)
Discussion started by: vijaykodukula
3 Replies

3. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

4. Shell Programming and Scripting

grep - Extracting multiple key words from stdout

Hello. From command line, the command zypper info nxclient return a bloc of data : linux local # zypper info nxclient Loading repository data... Reading installed packages... Information for package nxclient: Repository: zypper_local Name: nxclient Version: 3.5.0-7 Arch: x86_64... (7 Replies)
Discussion started by: jcdole
7 Replies

5. Shell Programming and Scripting

Max of Date DD-MM-YYYY 17:30

Hi, I have two sets of dates as below. Need to find the maximum of them. 14-Feb-2006 17:30,02-Feb-2006 14:46 14-Feb-2006 17:30,02-Feb-2006 14:46 14-Feb-2006 17:30,02-Feb-2006 14:46 23-May-2006 18:25,02-Feb-2006 14:46 13-Feb-2006 12:00,02-Feb-2006 14:46 23-May-2006 18:25,02-Feb-2006... (4 Replies)
Discussion started by: ramesh12621
4 Replies

6. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

7. Shell Programming and Scripting

extracting KEY from a line

consider a line from a file is stored in a variable called $var i.e. var=" KEY `controllingZoneId` (`controllingZoneId`)," or it can be (i.e without comma at the end) var="KEY `requestingUserId` (`requestingUserId`)" here i need to extract KEY and 1st and 2nd word i want to know how... (7 Replies)
Discussion started by: vivek d r
7 Replies

8. Shell Programming and Scripting

extracting row with max column value using awk or unix

Hello, BC106081_abc_128240811_128241377 7.96301 BC106081_abc_128240811_128241377 39.322 BC106081_cde_128240811_128241377 1.98628 BC106081_def_128240811_128241377 -2.44492 BC106081_abc_128240811_128241377 69.5504 FLJ00075_xyz_14406_16765 -0.173417 ... (3 Replies)
Discussion started by: Diya123
3 Replies

9. UNIX for Dummies Questions & Answers

Extracting a Private key from a keystore?

Hi everyone! I know you can extract public keys from a keystore using the keytool command. But what is the process to extract a private key from a jks keystore and import into another jks keystore using keytool? Any guidance would be greatly appreciated! I can't seem to find anything, I do... (0 Replies)
Discussion started by: Keepcase
0 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question