Calculate


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calculate
# 1  
Old 05-01-2013
Calculate

i have file input

Code:
abcedef|wert|13|03|10|04|23|A1|13|05|01|09|31
fsdasdf|ferg|12|04|25|21|21|A1|13|02|26|20|31
dfsfsad|gerg|12|04|25|21|21|A1|13|02|25|25|31

i expect the output

Code:
abcedef|wert|13|03|10|04|23|A1|13|05|01|09|31|9.516666667
fsdasdf|ferg|12|04|25|21|21|A1|13|02|26|20|31|20.51666667
dfsfsad|gerg|12|04|25|21|21|A1|13|02|25|25|31|4.166666667

---------- Post updated at 10:41 PM ---------- Previous update was at 10:35 PM ----------

column 14 comes from

when column 11 != column 5 do (column12 + (column 13/60))
if else do ( column12-column6 +( (column13-column7)/60))
# 2  
Old 05-01-2013
Please show exactly where you're stuck so that we can help.
# 3  
Old 05-01-2013
Here is one way to do it:
Code:
$ awk 'BEGIN {FS=OFS="|"; CONVFMT=OFMT="%.10g"} {$14 = ($11 != $5 ? ($12 + ($13 / 60)) : ($12 - $6 + (($13 - $7) / 60))); print}' input
abcedef|wert|13|03|10|04|23|A1|13|05|01|09|31|9.516666667
fsdasdf|ferg|12|04|25|21|21|A1|13|02|26|20|31|20.51666667
dfsfsad|gerg|12|04|25|21|21|A1|13|02|25|25|31|4.166666667

# 4  
Old 05-01-2013
Mr Hanson,

i find the value 'negative" when add data like this (line4)

Code:
abcedef|wert|13|03|10|04|23|A1|13|05|01|09|31
fsdasdf|ferg|12|04|25|21|21|A1|13|02|26|20|31
dfsfsad|gerg|12|04|25|21|21|A1|13|02|25|25|31
wqeqwee|wewe|13|02|01|23|52|A1|13|05|01|09|31

so I need the output for column 14:

column 14 comes from

when column 11 != column 5 do (column12 + (column 13/60))
when column 11=column 5 and column12<=column6 do (column12 + (column 13/60))
when column 11=column 5 and column12>column6 do ( column12-column6 +( (column13-column7)/60))

pls help

---------- Post updated at 11:47 PM ---------- Previous update was at 11:42 PM ----------

expected output

Code:
abcedef|wert|13|03|10|04|23|A1|13|05|01|09|31|9.516666667
fsdasdf|ferg|12|04|25|21|21|A1|13|02|26|20|31|20.51666667
dfsfsad|gerg|12|04|25|21|21|A1|13|02|25|25|31|4.166666667
wqeqwee|wewe|13|02|01|23|52|A1|13|05|01|09|31|9.516666667

# 5  
Old 05-01-2013
Here's one way to do it with the extra logic. I put in separate file, because getting more complex and not easy to understand on single line.
Code:
$ cat test.awk
BEGIN { FS=OFS="|"; CONVFMT=OFMT="%.10g" }
$11 != $5              { $14 = ($12 + ($13 / 60)) }
$11 == $5 && $12 <= $6 { $14 = ($12 + ($13 / 60)) }
$11 == $5 && $12 > $6  { $14 = ($12 - $6 + (($13 - $7) / 60)) }
{ print }

Code:
$ awk -f test.awk input
abcedef|wert|13|03|10|04|23|A1|13|05|01|09|31|9.516666667
fsdasdf|ferg|12|04|25|21|21|A1|13|02|26|20|31|20.51666667
dfsfsad|gerg|12|04|25|21|21|A1|13|02|25|25|31|4.166666667
wqeqwee|wewe|13|02|01|23|52|A1|13|05|01|09|31|9.516666667

# 6  
Old 05-01-2013
big thanks,..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate datediff

I'm trying to calculate datediff between string from filename and current date, i got problem with storing variable from date command now=$(date +"%s") echo "NOW = ${now}"; file=$(ls -1Ap *.txt|grep -v /\$ |tail -1) echo "file name is $file" #filename 201710030549.txt y=$(echo... (3 Replies)
Discussion started by: before4
3 Replies

2. Shell Programming and Scripting

How to calculate difference:?

Experts, file1 : Want to find the difference of $3 field from next line's 3rd field, The difference to be calculated from next lines 3rd field, to current lines lines 3rd field. file1 : Jun24_2013.06242013 3301244928 3133059904 167370640 95% Jun25_1124.06252013 3301244928... (4 Replies)
Discussion started by: rveri
4 Replies

3. Shell Programming and Scripting

Calculate the mean of numbers

I have a text file which has 3 columns. The first two columns are shown below. 57 7 23 8 14 17 23 1 I need to print mean using the following formula . mean =sum of the numbers of $2/total numbers of $1 output:- sum of $2 = 33 total numbers = 4 mean= 8.25 (1 Reply)
Discussion started by: andreaalex
1 Replies

4. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

5. Shell Programming and Scripting

Calculate P Value -Awk

Is there any awk command to calculate P Value ?(Probability) Is it possib;e to calculate P va;ue for this data for ex? 7.891284 8.148193 7.749575 7.958188 7.887702 7.714877 8.141548 7.51845 8.27736 7.929853 7.92456 8.249126 7.989113 8.012573 8.351206 (2 Replies)
Discussion started by: stateperl
2 Replies

6. Shell Programming and Scripting

How To Calculate

I have 2 variables in my shell scripts in which i am using awk and calculating 2 files and getting 2 different variable called in_total and out_total. I want to subtract one variable from another so plz tell me how i can do that. Example is: cat in_file | awk -F: '{ in_total += $1 * 86400... (3 Replies)
Discussion started by: krishna_sicsr
3 Replies

7. Programming

calculate average

I have a file which is 2 3 4 5 6 6 so i am writing program in C to calculate mean.. #include<stdio.h> #include<string.h> #include <math.h> double CALL mean(int n , double x) main (int argc, char **argv) { char Buf,SEQ; int i; double result = 0; FILE *fp; (3 Replies)
Discussion started by: cdfd123
3 Replies

8. Shell Programming and Scripting

calculate the space

Hi everyone, I need to write a script to calculate the space for sub-folders under /home: Here is the scanrio: cd /home drwxr-xr-x 57 root root 8192 Jan 22 16:13 home_1 drwxrwxrwx 69 root root 8192 Jan 29 10:36 home_2 drwxr-xr-x 97 root root 8192 Nov... (8 Replies)
Discussion started by: za_7565
8 Replies

9. AIX

calculate time

Hi, How do I calculate time? I need to create an alert if a process is running more than 30 minutes. I need to get the first time and then get another, calculate it if more than 30 mins and then alert it to pager. Can't find it in internet. Thanks in advance, itik (2 Replies)
Discussion started by: itik
2 Replies

10. Shell Programming and Scripting

calculate from three files

Hi all I have 3 three files like: file1: 1|100 2|200 3|300 4|400 5|500 file2: 1|200 2|200 3|600 4|800 file3: 1|300 2|100 (5 Replies)
Discussion started by: koti_rama
5 Replies
Login or Register to Ask a Question