Locating the largest number and performing division


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Locating the largest number and performing division
# 1  
Old 09-09-2013
Locating the largest number and performing division

I have a tab delimited file with the following format

Code:
1 r 109 45 3 5 6 7
2 f  300 249 5 8 10
3 g 120 4 5 110 0
4 t 400 300 250 0 0
.....
.....
100,000 lines

I would like to get the largest number in columns 4, 5, 6, 7, 8 and divide that largest number with the number in column 3.

Desired output
Code:
1 r 109 45 3 5 6 7 0.41
2 f  300 249 5 8 10 0.83
3 g 120 4 5 110 0 0.9166
4 t 400 300 250 0 0 0.75

I tried excel today this but since it has over 100,000 lines, it is difficult to do it. Please let me know the best way to do it awk or see.
# 2  
Old 09-09-2013
Code:
awk '{ for(N=3; N<=8; N++) if((A[N] == "") || (A[N]<$N)) A[N]=$N }
END {
        for(N=4; N<=8; N++) printf("%f / %f = %f\n", A[N], A[3], A[N]/A[3]);
}' inputfile

# 3  
Old 09-09-2013
I guess this is what you want:
Code:
awk '{max=""; for (i=4; i<=NF; i++) if ($i>max) max=$i; print $0, max/$3}' file
1 r 109 45 3 5 6 7 0.412844
2 f  300 249 5 8 10 0.83
3 g 120 4 5 110 0 0.916667
4 t 400 300 250 0 0 0.75

This User Gave Thanks to RudiC For This Post:
# 4  
Old 09-12-2013
Printing output to a tab delimited file in awk

I wanted to print an output file as a tab delimited file. I tried the following:

Code:
 awk -F $ '\t' '{ print $1,$2,$3,$4+$5,$6,$7,$8,$9;}' inputfile > outputfile.txt

But the output is not tab delimited. Let me know
# 5  
Old 09-12-2013
Quote:
Originally Posted by Kanja
I wanted to print an output file as a tab delimited file. I tried the following:

Code:
 awk -F $ '\t' '{ print $1,$2,$3,$4+$5,$6,$7,$8,$9;}' inputfile > outputfile.txt

But the output is not tab delimited. Let me know
Read about the OFS variable in your AWK manual page.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Largest number in array. Help!

I need to calculate the biggest number in array size n. Example: Users enter: 1 7 4 9 The biggest number is : 9 Simple but I'm really new on this on Shell/Bash! Anything will be helpful! Thanks! #!/bin/bash printf "\tEnter a list of numbers, with spaces: " read -a ARRAY BIG=$1... (5 Replies)
Discussion started by: Sundown
5 Replies

2. Shell Programming and Scripting

Taking largest (negative) number from column of coordinates and adding positive form to every other

Hello all, I'm new to the forums and hope to be able to contribute something useful in the future; however I must admit that what has prompted me to join is the fact that currently I need help with something that has me at the end of my tether. I have a PDB (Protein Data Bank) file which I... (13 Replies)
Discussion started by: crunchgargoyle
13 Replies

3. Shell Programming and Scripting

awk second largest, third largest value

I have two text files like this: file1.txt: 133 10 133 22 133 13 133 56 133 78 133 98 file2.txt: 158 38 158 67 158 94 158 17 158 23 I'm basically trying to have awk check the second largest value of the second column of each text file, and cat it to its own text file. There... (13 Replies)
Discussion started by: theawknewbie
13 Replies

4. HP-UX

Division returning a negative number

Hi All, Just faced an interesting thing in HP-UX. I was dividing 2955334616 / 2 by using echo `expr 2955334616 / 2` , and this ofcourse which expects 1477667308 to be returned. But I am getting -669816340 and I am :wall: how exactly this is possible. It is not one of the compliments (Ones or... (4 Replies)
Discussion started by: mail2sanand
4 Replies

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

6. Shell Programming and Scripting

checking the smallest and largest number

Hi All, My script is reading a log file line by line log file is like ; 19:40:22 :INFO Total time taken to Service External Request---115ms 19:40:25 DEBUG : Batch processed libdaemon.x86_64 0-0.10-5.el5 - u 19:40:22 INFO Total time taken to Service External Request---20ms 19:40:24... (4 Replies)
Discussion started by: subin_bala
4 Replies

7. Shell Programming and Scripting

search the largest number and duplicates string

Hi, My input file contain list of username, and it may have name with number as a suffix (if duplicated). Ex: mary john2 mike john3 john5 mary10 alexa So i want to check with a specific username (without suffix number) how many duplicated name, and what is the... (13 Replies)
Discussion started by: fongthai
13 Replies
Login or Register to Ask a Question