LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2015, 11:21 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Trying to get max and min for each entry in a table


Hi all,

I have a table of that looks like this:
Code:
85986601_[HSAP] 422 681
evm.model.scaffold44.22_[AJAP] 38 293
evm.model.scaffold101.42_[AAUS] 462 682
evm.model.scaffold101.42_[AAUS] 404 459
evm.model.scaffold101.42_[AAUS] 40 94
Sakowv30013372m_[SKOW] 12 271
390369827_XP_003731720.1_[SPUR] 20 190
I want to produce a "condensed" version of this file with just one line per unique column 1 entry. In some cases there are multiple identical entries in column 1. In those cases, I want to print column 1 and then the minimum value of column 2 and maximum value of column 3.

The desired output from this small example would be:
Code:
85986601_[HSAP] 422 681
evm.model.scaffold44.22_[AJAP] 38 293
evm.model.scaffold101.42_[AAUS] 40 682
Sakowv30013372m_[SKOW] 12 271
390369827_XP_003731720.1_[SPUR] 20 190
Any advice would be greatly appreciated.

Thank you!
Kevin
 
Old 11-26-2015, 12:39 AM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
awk seems to be the best tool. You can set up two associative arrays, one for the minima, the other for the maxima, and indexed by the first column.

The shell also features associative arrays, but I have no experience with them.
 
Old 11-28-2015, 01:53 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I'd Perl it, using a hash (aka assoc array), but you only need one entry per col 1 key, as you can nest hashes (or you can store the max & min values in an array as the hash 'value' )
 
Old 11-28-2015, 08:43 PM   #4
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Code:
cat kmkocot.file
85986601_[HSAP] 422 681
evm.model.scaffold44.22_[AJAP] 38 293
evm.model.scaffold101.42_[AAUS] 462 682
evm.model.scaffold101.42_[AAUS] 404 459
evm.model.scaffold101.42_[AAUS] 40 94
Sakowv30013372m_[SKOW] 12 271
390369827_XP_003731720.1_[SPUR] 20 190
Code:
#!/usr/bin/perl

# kmkocot.pl

use strict;
use warnings;
# max and min tester functions
use List::Util qw(max min);

# keep unique lines
my %records;
# keep a record of in which order a line appears
my @order;

# read file from command line
while(<>){
    # separate line into three tokens
    my ($id, $min, $max) = split;
    # test max and min if there is an entry for an id already
    if(exists $records{$id}){
        $records{$id}[0] = min($records{$id}[0], $min);
        $records{$id}[1] = max($records{$id}[1], $max);
    }
    else{
        # first time the id is seen, keep the order
        push @order, $id;
        # record current min and max
        push @{$records{$id}}, ($min, $max);
    }
}

# display records result
for my $id (@order){
    print "$id @{$records{$id}}\n";
}
Code:
perl kmkocot.pl kmkocot.file
85986601_[HSAP] 422 681
evm.model.scaffold44.22_[AJAP] 38 293
evm.model.scaffold101.42_[AAUS] 40 682
Sakowv30013372m_[SKOW] 12 271
390369827_XP_003731720.1_[SPUR] 20 190

Last edited by Aia; 11-28-2015 at 10:01 PM. Reason: substitute custom max and min functions for List::Util
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl to find max and min values eminempark Programming 1 01-09-2013 05:45 AM
min-max-x buttons to left? danbuter Bodhi 6 02-15-2011 08:06 AM
No Max/Min/Close Button in Ubuntu 9.10 newbieinubuntu Ubuntu 2 12-31-2009 04:01 AM
Min/Max buttons? andykap Linux - General 3 12-15-2003 01:09 AM


All times are GMT -5. The time now is 06:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration