Calculate different between two columns and return high and lower value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate different between two columns and return high and lower value
# 1  
Old 04-08-2010
Error Calculate different between two columns and return high and lower value

In database.txt
Code:
tourID:name:buyin:buyout
0001:SundayMillion:210:1000000
0002:MondayMillion:115:114
0003:Tuesday:123:0
0004:Thrusday:12412:26

I want the outpout:
Code:
The higher value is SundayMillion, proffit 999790 euros.
The lower value is  MondayMillion, proffit 1 euros.
The value more negative is Tuesday, proffit is negative, -123 euros.

I want the difference between $buyin and $buyout in absolute and return the high and lower value.

Thanks
# 2  
Old 04-08-2010
Assuming there's no column header line in the actual file.
Not sure if your last metric is correct - my understanding gives me 'Thursday'..

nawk -f raf.awk myFile
raf.awk:
Code:
BEGIN {
  FS=":"
}

{
   diffR=$4-$3
   diff=(diffR<0)?-diffR:diffR
   if (higher<diff) {
       higher=diff
       higherA=$2
   }
   if (lower>diff ||FNR==1){
       lower=diff
       lowerA=$2
   }

   if (lowerR>diffR ||FNR==1){
       lowerR=diffR
       lowerRA=$2
   }

}
END {
   printf("The higher value is %s, proffit %d euros.\n", higherA, higher)
   printf("The lower value is %s, proffit %d euros.\n", lowerA, lower)
   printf("The value more negative is %s proffit is negative, %d euros.\n", lowerRA, lowerR)
}


Last edited by vgersh99; 04-08-2010 at 05:52 PM.. Reason: ooops - fat fingers
# 3  
Old 04-08-2010
Code:
./v12.sh: line 369: nawk: command not found

this is why?
# 4  
Old 04-08-2010
Quote:
Originally Posted by rafazz
Code:
./v12.sh: line 369: nawk: command not found

this is why?
use awk or gawk instead.
# 5  
Old 04-08-2010
it works with gawk.

Higher works fine but the lower shows me the header of my file:
Code:
tourID:name:buyin:buyout

how can i despite the first line?
# 6  
Old 04-08-2010
Code:
BEGIN {
  FS=":"
}

FNR!=1{
   diffR=$4-$3
   diff=(diffR<0)?-diffR:diffR
   if (higher<diff) {
       higher=diff
       higherA=$2
   }
   if (lower>diff ||FNR==2){
       lower=diff
       lowerA=$2
   }

   if (lowerR>diffR ||FNR==2){
       lowerR=diffR
       lowerRA=$2
   }

}
END {
   printf("The higher value is %s, proffit %d euros.\n", higherA, higher)
   printf("The lower value is %s, proffit %d euros.\n", lowerA, lower)
   printf("The value more negative is %s proffit is negative, %d euros.\n", lowerRA, lowerR)
}

# 7  
Old 04-08-2010
Thanks thanks thanks!! It works=))))
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate percentage difference between two columns

I have a input text file in this format: ITEM1 10.9 20.1 ITEM2 11.6 12 ITEM3 14 15.7 ITEM5 20 50.6 ITEM6 25 23.6 I want to print those lines which have more than 5% difference between second and third columns. (8 Replies)
Discussion started by: ctrld
8 Replies

2. Shell Programming and Scripting

Match first two columns and calculate percent of average in third column

I have the need to match the first two columns and when they match, calculate the percent of average for the third columns. The following awk script does not give me the expected results. awk 'NR==FNR {T=$3; next} $1,$2 in T {P=T/$3*100; printf "%s %s %.0f\n", $1, $2, (P>=0)?P:-P}' diff.file... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

3. Shell Programming and Scripting

Return first two columns if match found among two files

Hi, I have FileA with one column. File B with 15 columns separated by comma delimiter. I need to compare the FILEA value with all 15 columns of FILEB... if matches, need to return the 1st, 2nd column values of FILEB. How to achieve this through shell script? Thanks in advance. (5 Replies)
Discussion started by: vamsikrishna928
5 Replies

4. Shell Programming and Scripting

Calculate percentage of columns greater than certain value in a matrix using awk

This matrix represents correlation values. Is it possible to calculate the percentage of columns (a1, a2, a3) that have a value >= |0.5| and report the percentage that has positive correlation >0.5 and negative correlation <-0.5 separately. thanx in advance! input name a1 a2 a3... (5 Replies)
Discussion started by: quincyjones
5 Replies

5. Shell Programming and Scripting

How to calculate average of two columns and copy into another file?

Hi, I need help with the awk command. I have a folder with aprox 500 files each one with two columns and I want to print in a new file, the average of column 1 and average of column 2 and the name of each file. Input files are: File-1: 100 99 20 99 50 99 50 99 File-2: 200 85... (3 Replies)
Discussion started by: Lokaps
3 Replies

6. Shell Programming and Scripting

Calculate the difference of two columns and keep the line with specific value

Hi everyone, I am trying to find sty all day. I have two files: File 1: N 82 AAA A 1 0.67 N 83 BBB B 1 0.79 N 84 CCC C 1 0.11 File 2: N 82 AAA A 1 0.63 N 83 BBB B 1 0.03 N 84 CCC C 1 0.08 I want to calculate... (2 Replies)
Discussion started by: Tzole
2 Replies

7. Shell Programming and Scripting

AWK: calculate ratio of columns

Hi all, I have a tab-delimited text file in which i have a few columns which look like, X Y U V 2 3 4 5 4 5 3 4 6 4 3 2 For example, I want to calculate the ratio (X+Y)/(X+Y+U+V) for each row and print the output. X Y U V ... (3 Replies)
Discussion started by: mehar
3 Replies

8. Red Hat

apache high cpu load on high traffic

i have a Intel Quad Core Xeon X3440 (4 x 2.53GHz, 8MB Cache, Hyper Threaded) with 16gig and 1tb harddrive with a 1gb port and my apache is causing my cpu to go up to 100% on all four cores heres my http.config <IfModule prefork.c> StartServers 10 MinSpareServers 10 MaxSpareServers 15... (4 Replies)
Discussion started by: awww
4 Replies

9. Shell Programming and Scripting

How to calculate the difference between two adjacent columns?

Dear All, I need to find the difference between two adjacent columns. The file is having 'i' columns and i need to find the difference between two adjacent columns (like $1 difference $2; $2 difference $3; .... and $(i-1) difference $i). I have used the following coding awk '{ for (i=1; i<NF;... (7 Replies)
Discussion started by: Fredrick
7 Replies

10. UNIX for Advanced & Expert Users

Sun: High kernel usage & very high load averages

Hi, I am seeing very high kernel usage and very high load averages on my system (Although we are not loading much data to our database). Here is the output of top...does anyone know what i should be looking at? Thanks, Lorraine last pid: 13144; load averages: 22.32, 19.81, 16.78 ... (4 Replies)
Discussion started by: lorrainenineill
4 Replies
Login or Register to Ask a Question