awk comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk comparison
# 1  
Old 08-25-2010
Question awk comparison

Hello all,

Probably a very simple question, I am stuck with a small part of a code:

I am trying to do a comparison to get the maximum value of column 6 if columns 1, 4 and 5 of two or more rows match. Here is what I am doing:

Code:
awk -F'\t' '{if ($6 > a[$1"\t"$4"\t"$5])a[$1"\t"$4"\t"$5]=$6}END{for (i in a) print i"\t"a[i]}'

But in the output I want to include the column 2 and 3 values for which column 6 value was the highest.

Important: All fields are tab separated.
For example,

Code:
111 aaa bbb ccc ddd 0.9
111 XYZ PQR ccc ddd 0.5
111 xyz pqr ccc ddd 0.7

will give
Code:
111 aaa bbb ccc ddd 0.9

Please suggest a fix to my solution or any solution that works.
# 2  
Old 08-25-2010
Save the whole line, or the fields you want, when you save the maximum.

Code:
awk -F'\t' '
{
   if( $6 > a[$1"\t"$4"\t"$5] )
   {
      a[$1"\t"$4"\t"$5] = $6  
      output[$1"\t"$4"\t"$5] = $0;   # saves the whole line 
  }
}
END{
   for (i in a) 
       print output[i];
}'


Last edited by agama; 08-26-2010 at 07:17 PM.. Reason: fixed typo
This User Gave Thanks to agama For This Post:
# 3  
Old 08-25-2010
Code:
# cat file
111     aaa     bbb     ccc     ddd     0.9
111     XYZ     PQR     ccc     ddd     0.5
111     xyz     pqr     ccc     ddd     0.7
# awk 'BEGIN{FS=OFS="\t"}{x=$1FS$4FS$5;if(!y[x]||y[x]<$6){y[x]=$6;z[x]=$0}}END{for(i in z){print z[i]}}' file
111     aaa     bbb     ccc     ddd     0.9

# 4  
Old 08-26-2010
agama's code has some problems, idea is right
Code:
 awk -F'\t' '{if ($6 > a[$1"\t"$4"\t"$5]){a[$1"\t"$4"\t"$5]=$6;b[$1"\t"$4"\t"$5]=$0 }}END{for (i in a) print b[i]}' $FILE


Last edited by jaysean; 08-26-2010 at 01:20 AM..
This User Gave Thanks to jaysean For This Post:
# 5  
Old 08-26-2010
Quote:
Originally Posted by jaysean
agama's code has some problems, idea is right
Serves me right for not running it. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk IF date comparison help

Hey everyone, I'm trying to create a script using awk and if that will list all of our aws tapes that have archived date that is past 90 days from todays current date, so that I can pass that to my aws command to remove. The fifth column is the creation date in epoch/seconds, so I'm... (13 Replies)
Discussion started by: beyondmondays
13 Replies

2. Shell Programming and Scripting

awk if comparison with variable

Hi , Need help, p for value in `awk -F, '{print $1 }' ad | uniq` do x=$(echo $value) echo $x echo `awk -F, '{if( $1 == $x) sum = sum + $8 } END{print sum}' ad` --- not working echo `awk -F, '{if($1 == “MT”) sum = sum + $8 } END{print sum}' ad` -- Working but hard coded done; ad... (4 Replies)
Discussion started by: nadeemrafikhan
4 Replies

3. Shell Programming and Scripting

File comparison using awk

Hi All, i have two files file1 ,file 2 file 1 col1|col2|col3|col4|col5|col6|col7|col8 11346925|0|2009-09-20|9999-12-31|100|0 11346925|0|2009-09-20|9999-12-31|120|0 12954311|0|2009-09-11|9999-12-31|100|0 12954311|0|2009-07-23|2999-12-31|120|0 12954312|0|2009-09-11|9999-12-31|100|0... (9 Replies)
Discussion started by: mohanalakshmi
9 Replies

4. Shell Programming and Scripting

awk comparison with two patterns

Here is my list, which contains URLs for file downloads: //servername.com/version/panasonic1,1_1.1.1 //servername.com/version/panasonic3,1_6.7.1 //servername.com/version/panasonic3,2_6.8 //servername.com/version/panasonic2,6_3.0.2 //servername.com/version/panasonic3,1_7.1.3... (5 Replies)
Discussion started by: ibanezpete
5 Replies

5. Shell Programming and Scripting

Need help with simple comparison in AWK

Hi, I'm new to AWK and I'm having problems comparing a field to a string variable. /ARTIST/ {x = $2} $1 ~ x {print $0}My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the... (7 Replies)
Discussion started by: klusps
7 Replies

6. Shell Programming and Scripting

AWK string comparison

Hey guys.. New in linux scripting and need some help on some scripting with history command. I managed to export the command history into a file and now i'm trying to select from that file some specific commands that were made in a certain period. Here's what i got so far echo -n... (2 Replies)
Discussion started by: mishu_cgm
2 Replies

7. Shell Programming and Scripting

File comparison using awk

my files are as follows fileA sepearated by tab /t 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB funnymou120112 funnymou234470 mou3raspnhdhv rddfgmoudone1438748 so all those record which are greater than 3 and which are not... (6 Replies)
Discussion started by: rajniman
6 Replies

8. Shell Programming and Scripting

awk comparison and substitution

Hi, here's my - not so easy to describe - problem: I want to compare the values of one file (FileA) with a cutoff-value and, if this comparison is true, substitute those values with those in the second file (FileB). However, there are many FileA's (FileA), whereas there is only one FileB. Every... (10 Replies)
Discussion started by: waddle
10 Replies

9. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

10. Shell Programming and Scripting

awk comparison with variable

hi, I want to compare i variable in the awk statement but its not working out. Pl help me out If we do the comparison like this its OK, cat sample | awk -F" ", '{if ($1=="1-Sep-2009") print $1,$2,$3,$4,$5}' But if u use a variable instead of "1-Sept-2009", it does not return anything,... (2 Replies)
Discussion started by: asadlone
2 Replies
Login or Register to Ask a Question