Find larger on replicates and output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find larger on replicates and output
# 1  
Old 01-13-2015
Find larger on replicates and output

Hi All
I have a question.
I have a file like this:
Code:
10 name1 ID1 value1 value2 valueN..
31 name2 ID1 value1 value2 valueN..
20 name3 ID2 value1 value2 valueN..
23 name4 ID2 value1 value2 valueN..
33 name5 ID2 value1 value2 valueN..
45 name6 ID2 value1 value2 valueN..

well, my output should be like this:
Code:
31 name2 ID1 value1 value2 valueN..
45 name6 ID2 value1 value2 valueN..

For each replicates ID (Column 3) I would like to output only the row in which the first column is larger.

Hope you can help me!

Thank you a lot!

Giuliano
# 2  
Old 01-13-2015
Hello giuliangiuseppe,

Could you please try following solution, kindly don't use previous solution as I haven't tested that in different cases, following may help you. I have removed previous not accurate solution too now.

Code:
awk 'FNR==NR{X[$3]=X[$3]>$1?X[$3]:$1;next} {D[$3 OFS $1]=$0} END{for(i in X){print D[i OFS X[i]]}}'  Input_file Input_file

EDIT: Adding a non one liner solution for same.
Code:
awk 'FNR==NR{
                X[$3]=X[$3]>$1?X[$3]:$1;
                next
            }
            {
                D[$3 OFS $1]=$0
            }
     END    {
                for(i in X){
                                print D[i OFS X[i]]
                           }
            }
    '  Input_file Input_file


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-13-2015 at 08:46 AM.. Reason: Removed previous solution which was not working in all test cases
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-13-2015
Code:
awk '($1>max[$3]) {max[$3]=$1; s[$3]=$0} END {for (i in s) print s[i]}' file

---------- Post updated at 02:52 PM ---------- Previous update was at 02:42 PM ----------

R.Singh, your solution has all information during the 2nd pass, so can directly print!
Code:
awk 'FNR==NR{
                X[$3]=X[$3]>$1?X[$3]:$1;
                next
            }
        (X[$3]==$1)
    '  Input_file Input_file

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Larger window

I would like the window that this opens to be larger. Can it be modified to do so? Thanks. #!/usr/bin/env python3 # Text snippets are in /home/andy/.config/snippet_paste/ import os import subprocess home = os.environ directory = home+"/.config/snippet_paste" if not... (4 Replies)
Discussion started by: drew77
4 Replies

2. Shell Programming and Scripting

Merge row based on replicates ID

Dear All, I was wondering if you may help me with an issue. I would like to merge row based on column 1. input file: b1 ggg b2 fff NA NA hhh NA NA NA NA NA a1 xxx a2 yyy NA NA zzz NA NA NA NA NA a1 xxx NA NA a3 ttt NA ggg NA NA NA NA output file: b1 ggg b2 fff NA NA hhh NA NA NA NA NA... (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

3. Shell Programming and Scripting

Find smallest between replicates ID

Hi All I need to find the smallest values between replicates id (column1) Input file: a name1 1200 a name2 800 b name1 100 b name2 150 b name3 4output: a name2 800 b name3 4 Do you have any suggestion? Thank you! (9 Replies)
Discussion started by: giuliangiuseppe
9 Replies

4. Shell Programming and Scripting

Find biggest values on replicates

Dear All I was wondering if someone of you know how to resolve an issue that I met. In particular I have a file like this: ENSMUSG01 chr1 77837902 77853530 ENSMUSG02 chr2 18780447 18811972 ENSMUSG02 chr2 18780453 18811626 ENSMUSG02 chr2 18807356 18811987 ENSMUSG03 chr3 142575634 142576538... (6 Replies)
Discussion started by: giuliangiuseppe
6 Replies

5. Shell Programming and Scripting

Output minimum and maximum values for replicates ID

Hi All I hope that someone could help me! I have an input file like this, with 4 colum(ID, feature1, start, end): a x 1 5 b x 3 10 b x 4 9 b x 5 16 c x 5 9 c x 4 8 And my output file should be like this: a x 1 5 b x 3 16 c x 4 9 What I would like to do is to output for each ID... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

6. Shell Programming and Scripting

Output Row if Third Column is Larger

Hi, I am fairly new to Unix scripting. We are running Solaris 5.10. I have the following question: Assume a text file with one text column, followed by 2 integer columns. How would I generate a script or, preferably, a command that will output the rows in which the value of the third... (6 Replies)
Discussion started by: QZ1
6 Replies

7. UNIX for Dummies Questions & Answers

Making replicates of a file with part of a line randomized for each file

Ok, so let's say that I have a file like the following: I want to create 100 replicates of this file, except that for each file, I want different randomized combinations of either A or B at the end of each line so that I would end up with files like the following: and etc. I... (1 Reply)
Discussion started by: Scatterbrain26
1 Replies

8. Shell Programming and Scripting

using grep for find values larger/less than

Hi I have been experimenting with grep to find values for a particular column that is greater than or less than certain #'s. So my file looks like this: name -2 2 name1 -2 2 name2 -1 4 name3 3 3 So I want to find rows with values less than or equal to -2 and those greater than... (5 Replies)
Discussion started by: phil_heath
5 Replies

9. UNIX for Dummies Questions & Answers

How to find a file whick is consuming larger disk space in file system

Hello, Can anybody please tell me the command to find out the filesystem or a file which is consuming larger disk space sing i want to find out the file and want to compress it please help me out any help would be appreciated (6 Replies)
Discussion started by: lokeshpashine
6 Replies

10. Solaris

Upgrading To Larger Disk?

I have just been assigned the task of upgrading to a larger disk on a e250; however, I am use to working on Linux and x86 hardware. I would be very appreciative if someone could inform me on how this procedure can be done safely. To begin with, the e250 has a 18 Gig primary scsi disk and a 18... (3 Replies)
Discussion started by: cstovall
3 Replies
Login or Register to Ask a Question