Problem facing to compare different column and print out record with smallest number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem facing to compare different column and print out record with smallest number
# 1  
Old 02-27-2014
Problem facing to compare different column and print out record with smallest number

Hi,

Input file 1 :
Code:
37170   37196   77      51
37174   37195   73      52
37174   37194   73      53

Desired Output file 1 :
Code:
37170   37196   77      51

Input file 2 :
Code:
37174   37195   73      0
37170   37196   77      0

Desired Output file 2 :
Code:
37174   37195   73      0

Input file 3 :
Code:
37170   37196   77      51

Desired Output file 3 :
Code:
37170   37196   77      51

I would like to print out the record with smallest number through compare the data in column 3 and 4.
If the smallest number are in same column (either column 3 or 4, Input file 2). In that case, I would like to compare the other column and print out the line that record smallest number in both column 3 and 4.

Below is the command that suggested by Subbeh and Klashxx,
Code:
awk '{x=$3<$4?$3:$4; i=i?i:x ;if(x<=i){i=x; s=$0}} END{print s}' infile
awk 'NR==1{L=$NF<$(NF-1)?$NF:$(NF-1)}$(NF-1)<= L{L=$(NF-1);t=$0};$NF<=L{L=$NF;t=$0}END{print t}' infile

It work fine for Input file 1 and Input file 3.
Unfortunately, it won't work for cases like Input file 2.

Thanks for any advice.
# 2  
Old 02-27-2014
This thread duplicates the thread Problem to print out record got smallest number in specific column.

This thread is closed.
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

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

3. Shell Programming and Scripting

Help with keep the smallest record in file

Input file US Score 10 UK Ball 20 AS Score 50 AK Ball 10 PZ Ballon 50 PA Score 70 WT Data 10 . . Desired output file US Score 10 AK Ball 10 WT Data 10 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. UNIX for Beginners Questions & Answers

Sort by record column, Compare with conditons and export the result

Hello, I am new to Unix and would like to seek a help, please. I have 2 files (file_1 and file_2), I need to perform the following actions. 1 ) Sort the both file by the column 26-36 (which is Invoice number) what is sort command with the column sort? 2) Compare the file_1.sorted and... (3 Replies)
Discussion started by: Usagi
3 Replies

5. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

6. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

7. 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

8. Shell Programming and Scripting

Print smallest negative number with corresponding index from a column

considering the following table: ID col1 col2 col3 col4 1 -16.06801249 13.49785832 -56.57087607 -27.00500526 2 -1.53315720 0.71731735 -42.03602078 -39.78554623 3 -1.53315190 0.71731587 -42.03601548 ... (3 Replies)
Discussion started by: Birda
3 Replies

9. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

10. UNIX for Dummies Questions & Answers

How to print largest and smallest number.

Hey. This is pretty easy stuff but I'm learning the basics of Unix at the moment so keep that in mind. I have to: 1) Write a C-shell script to monitor user activity on the server for 13 minutes. 2) Then print the smallest and largest number of users during these 13 minutes. I have this: 1)... (2 Replies)
Discussion started by: amp10388
2 Replies
Login or Register to Ask a Question