numbers comparison in fields of a file and print least value of them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting numbers comparison in fields of a file and print least value of them
# 1  
Old 01-24-2011
numbers comparison in fields of a file and print least value of them

Hi ,

I'm trying to compare fields in the file, I want compare the numbers in each column and get the least value of it.

Code:
> cat input_file
0.0000  0.0000  0.0000  0.0000
0.0000  0.0000  0.0000  0.0000
-0.2050 -0.6629 -0.6407 -0.6599
-0.4085 -0.3959 -0.2526 -0.3597
0.3439  0.2275  0.2780  0.2216
0.4339  0.3675  0.2780  0.4616

Code:
> cat output_file
0.0000
0.0000
-0.2050
-0.2526 
0.2216
0.2780

Thanks.
# 2  
Old 01-24-2011
Generic solution using perl:
Code:
perl -ne 'chop;print ((sort(split))[0]."\n");' input_file

# 3  
Old 01-24-2011
Code:
 ruby -ane 'puts $F.min' file

# 4  
Old 01-25-2011
Hi Kurumi,

Many Thanks, it is cool. I have not heard of "ruby" in the past. I have a small change in the need.

For columns with negative numbers, I want the biggest negative number. For columns with positive numbers, I need the smallest number. Cases with mix of positive and negative, i'm fine with the biggest negative number.

Code:
> cat input_file
0.0000  0.0000  0.0000  0.0000
0.0000  0.0000  0.0000  0.0000
-0.2050 -0.6629 -0.6407 -0.6599
-0.4085 -0.3959 -0.2526 -0.3597
0.3439  0.2275  0.2780  0.2216
0.4339  0.3675  0.2780  0.4616
0.0010 -0.0019  -0.0100 0.0100

Code:
> cat output_file
0.0000
0.0000
-0.6629 
-0.4085 
0.2216
0.2780
-0.0100

Thanks.
# 5  
Old 01-25-2011
Try this,

Code:
perl -ne 'chomp;print ((sort{$a<=>$b}(split))[0]."\n");' inputfile

# 6  
Old 01-25-2011
MySQL

That did the trick !!!

Thanks Pravin / Kurumi / otheus.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

File Comparison: Print Lines not present in another file

Hi, I have fileA.txt like this. B01B02 D0011718 B01B03 D0012540 B01B04 D0006145 B01B05 D0004815 B01B06 D0012069 B01B07 D0004064 B01B08 D0011988 B01B09 D0012071 B01B10 D0005596 B01B11 D0011351 B01B12 D0004814 B01C01 D0011804 I want to compare this against another file (fileB.txt)... (3 Replies)
Discussion started by: genehunter
3 Replies

7. Shell Programming and Scripting

Print numbers along with file names.

Hi All, I have some thousand files with names like 1.syl, 2.syl, 5.syl etc. These files contain one sentence each. I want to store all those sentences along with the file ID that is 1, 2, 5 with the sentences they contain. For example, 1.syl has this is a test line 2.syl has ... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

Comparison of fields in Files

Hello, I have two files with tab delimited data. The file will contain details something like below: FILENAME.A.B.C. 3 5 VALID PROCESSED I would have a bench mark file. I would be getting new files of the same format. My requirement is to compare a particular field for a... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

9. UNIX for Dummies Questions & Answers

AWK ??-print for fields within records in a file

Hello all, Would appreciate if someone can help me out on the following requirement. INPUT FILE: -------------------------- TPS REPORT abc def ghi jkl mon pqr stu vrs lll END OF TPS REPORT TPS REPORT field1 field2 field3 field4 field5 field6 (8 Replies)
Discussion started by: hyennah
8 Replies

10. Shell Programming and Scripting

how to print out line numbers of a text file?

i have this text file name test.txt which contain : aaaaa bbb iiiiiiiiiiiii ccf ddaaa ddd and i need a script that can print out the line numbers using a while loop.. so when the script is run..it will have this: 1 2 3 any ideas? :) thanks guys (4 Replies)
Discussion started by: forevercalz
4 Replies
Login or Register to Ask a Question