print least value of a column on the basis of another column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print least value of a column on the basis of another column
# 1  
Old 08-18-2011
print least value of a column on the basis of another column

Hi,

I am new to linux...

I have a file which looks like:

Quote:
9 ALA 9 ALA 0.00000
9 ALA 8 VAL 2.98760
9 ALA 7 GLN 3.89079
9 ALA 4 LEU 15.117463
9 ALA 5 GLN 12.218415
9 ALA 6 GLN 8.719419
9 ALA 7 PRO 5.753082
16 ALA 16 ALA 0.000000
16 ALA 17 VAL 6.449406
16 ALA 18 GLN 3.789670
16 ALA 4 LEU 25.908312
16 ALA 5 GLN 23.866964
16 ALA 6 GLN 20.316647
16 ALA 11 KHG 8.403637
16 ALA 8 GLY 18.967777
16 ALA 9 ALA 15.583528
I want to print the entire row in which 5th column is having minimum value for every first column (i.e min for 9 and min for 16). Along with the condition [ if ($1 != $3) && ($3 != $1 +1) && ($3 != $1-1.

The output should be like :

Quote:
9 ALA 7 PRO 5.753082
16 ALA 7 PRO 8.403637
For finding maximum value in last column for every value in column1, I used the below code:

Code:
awk -F" " 'b[$1] < $5 {b[$1]=$5; a[$1]=$0} END {for (i in a) {print a[i]}}' inputfile > outputfile

but, the reverse of it for finding minimum value is not working....
Code:
awk -F" " 'b[$1] > $5 {b[$1]=$5; a[$1]=$0} END {for (i in a) {print a[i]}}' inputfile > outputfile

Smilie


Any help is highly appreciated.....
# 2  
Old 08-18-2011
Try:
Code:
awk '{if(m[$1]>$5||a[$1]==""){a[$1]=$0;m[$1]=$5}}END{for (i in a){print a[i]}}' file

PS. for your sample data, correct output seems to be:
Code:
9 ALA 9 ALA 0.00000
16 ALA 16 ALA 0.000000

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-18-2011
Your samples and your description are contradictory. Try this:
Code:
awk -F" " '!b[$1] || ( b[$1] > $5 && $1 != $3 && $3 != $1 +1 && $3 != $1-1) 
    {b[$1]=$5; a[$1]=$0} END {for (i in a) {print a[i]}}' INPUTFILE
9 ALA 8 VAL 2.98760
16 ALA 18 GLN 3.789670

Oops. The right code:
Code:
awk -F" " '!b[$1]++ || (b[$1] > $5 && $1 != $3 && $1 != $3+1 && $1 != $3-1) 
  {b[$1]=$5; a[$1]=$0} 
  END {for (i in a) {print a[i]}}' INPUTFILE
9 ALA 7 PRO 5.753082
16 ALA 18 GLN 3.789670


Last edited by yazu; 08-18-2011 at 02:10 PM..
# 4  
Old 08-18-2011
Thanks a lot bartus11.....

ur code is working fine.....

u r correct, the output shows

9 ALA 9 ALA 0.00000
16 ALA 16 ALA 0.000000

Here, I want to apply if condition in which, i dont want to read the lines where column 1 and column3 are same, column 3 is one number higher than in the column1 and column 3 is one number less than in the column1:

Code:
 
filenameme=liist
exec<$filename
while read line
do
 
if (column 3 != column1) && (column 3 != column1 +1) && (column3 != column1-1) 
{
awk '{if(m[$1]>$5||a[$1]==""){a[$1]=$0;m[$1]=$5}}END{for (i in a){print a[i]}}' $line
}
 
done

Is this way correct ???? or this "if" condition must come within awk statement??
# 5  
Old 08-18-2011
This should work:
Code:
awk '{if((m[$1]>$5||a[$1]=="")&&($1!=$3)&&($3!=$1+1)&&($3!=$1-1)){a[$1]=$0;m[$1]=$5}}END{for (i in a){print a[i]}}' file

# 6  
Old 08-18-2011
Thanks yazu and Bartus Smilie

ur code helped me a lot.....Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Group/concatenate certain column and basis on this do addition on other column

Hi Experts, Need your support I want to group/concatenate column 1,2,12 and 13 and if found duplicate then need to sum value of column 17,20,21 and column22. After concatenation if found unique then no action to be taken. Secondly want to make duplicate rows basis on grouping/concatenation of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. Shell Programming and Scripting

print least value of a column on the basis of another column

Hi, I am new to linux... I have a file which looks like: I want to print the entire row in which 5th column is having minimum value for every first column (i.e min for 9 and min for 16). Along with the condition awk -F" " 'b < $5 {b=$5; a=$0} END {for (i in a) {print a}}' inputfile >... (1 Reply)
Discussion started by: CAch
1 Replies

9. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question