简体   繁体   中英

use awk to confront first number in every line of a file and print the whole line that contains the largest one

The file I have looks like this: (thanks to @fredtantini)

 37.36      44.22    -221.12       0.85       2.02       4.00       0.49   2345.147  '/home/~/gold_soln_x_m1_70.sdf'                     'X'    
 38.46      45.89    -229.45       1.49       1.94       4.00       0.61   2370.912  '/home/~/gold_soln_y_m1_69.sdf'                     'Y'    
 39.90      46.86    -234.28       0.29       2.66       4.00       1.00   2368.052  '/home/~/gold_soln_w.sdf'                     'W'    
 37.75      48.10    -240.50       2.58       3.77       4.00       0.60   2220.947  '/home/~/gold_soln_z.sdf'                     'Z'

I need to have as output:

 39.90      46.86    -234.28       0.29       2.66       4.00       1.00   2368.052  '/home/~/gold_soln_w.sdf'                     'W'

printed in another file but I've never used awk so I have no idea on how to organize my script. looking for answers I found this:

awk'
function max(x){i=0;for(val in x){if(i<=x[val]){i=x[val];}}return i;}
{a[$2]=$2;next}`
END{minimum=min(a);maximum=max(a);print "Maximum = "maximum "}'

but it's not working and i have no idea why.

I've tried @fredtantini answer and it works from terminal but i need to insert it in a script:

The complete script looks like this:

#!/bin/bash
cd /home/~/
        for k in $( cat lista_sottocartelle ); do
            cd /home/~/$k 
            awk 'BEGIN{max=0}{if($1>max){max=$1;maxline=$0}}END{print maxline}' bestranking.lst >> indirizzi_ASP.lst

The output is: syntax error: unexpected end of file

Edit

I just forgot to add done at the end of the script. thanks to all of you.

Simply use:

~$ awk 'BEGIN{max=0}{if($1>max){max=$1;maxline=$0}}END{print maxline}' f
 39.90      46.86    -234.28       0.29       2.66       4.00       1.00   2368.052  '/home/~/gold_soln_w.sdf'                     'W'

For each line, you check if first number is greater than current max. If it is, you save the line in maxline . At the end, you print this line.

A shorter version doing the same thing (thanks to @Jotne):

awk '$1>max {max=$1;maxline=$0}END{print maxline}'

There are several problems in your script ( $2 is the second column, min function is not defined…)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM