Compute value from more than three consecutive rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compute value from more than three consecutive rows
# 8  
Old 04-18-2016
@Mr. Cragun and Ravinder, thank you for help!

After I switch to bash, I did get some output with the limited input I gave. However, there is problem using the input below:

Code:
site Date time value1 value2
0023 2014-01-01 00:00 32.0 23.7
0023 2014-01-01 01:00 38.0 29.9
0023 2014-01-01 02:00 85.0 26.6
0023 2014-01-01 03:00 34.0 25.3
0023 2014-01-01 04:00 37.0 23.8
0023 2014-01-01 05:00 80.0 20.3
0023 2014-01-01 06:00 90.0 20.0
0023 2014-01-01 07:00 180.0 20.0
0023 2014-01-01 08:00 30.0 20.
0023 2014-01-01 09:00 180.0 20.0
0023 2014-01-01 10:00 130.0 2
0023 2014-01-01 11:00 110.0 2
0023 2014-06-30 23:00 200.0 30.3
0023 2014-07-01 00:00 303.0 30.3
0023 2014-07-01 01:00 134.0 30.3

The output only gave:
Code:
site Date time value1 value2
0023 2014-01-01 05:00 80.0 20.3 1
0023 2014-01-01 06:00 90.0 20.0 2
0023 2014-01-01 07:00 180.0 20.0 3
0023 2014-01-01 09:00 180.0 20.0  4
0023 2014-01-01 10:00 130.0 2 2 5
0023 2014-01-01 11:00 110.0 2 2  6

But the desired output should be:

Code:
0023 2014-01-01 05:00 80.0 20.3      1
0023 2014-01-01 06:00 90.0 20.0      1
0023 2014-01-01 07:00 180.0 20.0    1
0023 2014-01-01 09:00 180.0 20.0    2
0023 2014-01-01 10:00 130.0 2        2
0023 2014-01-01 11:00 110.0 2        2 
0023 2014-06-30 23:00 200.0 30.3 1
0023 2014-07-01 00:00 303.0 30.3 1
0023 2014-07-01 01:00 134.0 30.3 1


---------- Post updated at 06:30 AM ---------- Previous update was at 05:51 AM ----------

@Rudic, thank you for help.
I am not sure if I understand you correctly or not, but there are tree lines that do match the requirement from the input:
Code:
0023 2014-01-01 05:00 80.0 20.3
0023 2014-01-01 06:00 90.0 20.0
0023 2014-01-01 07:00 180.0 20.0


Last edited by kathy wang; 04-18-2016 at 06:16 PM..
# 9  
Old 04-18-2016
They don't:
Quote:
... this pattern last for more than 3 hours continually...
which is four consecutive lines. What you mean (I guess) is "more than or equal"; change the script accordingly as indicated.
# 10  
Old 04-18-2016
RudiC, thank you. I had edited that as suggested. Here is the output I got when I use bash
Code:
awk: division by zero
 input record number 1, file input
 source line number 12

# 11  
Old 04-18-2016
Quote:
Originally Posted by kathy wang
RudiC, thank you. I had edited that as suggested. Here is the output I got when I use bash
Code:
awk: division by zero
 input record number 1, file input
 source line number 12

Change the 1st line of RudiC's script from:
Code:
awk '

to:
Code:
awk '
NR == 1         {next
                }

to skip over the header line in your input file.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 04-19-2016
Thank you, Runic and Don Cragun, I had tried the new code as suggested, but there is a problem. If I use the new input as below:010730023 2014-06-30 07:00 78.0 21.8081
Code:
010730023 2014-06-30 08:00 80.0 21.8081
010730023 2014-06-30 09:00 80.0 21.8081
010730023 2014-06-30 10:00 78.0 21.8081
010730023 2014-06-30 11:00 92.0 21.8081
010730023 2014-06-30 12:00 89.0 21.8081
010730023 2014-06-30 13:00 83.0 21.8081
010730023 2014-06-30 14:00 80.0 21.8081
010730023 2014-06-30 15:00 82.0 21.8081
010730023 2014-06-30 16:00 88.0 21.8081
010730023 2014-06-30 17:00 89.0 21.8081
010730023 2014-06-30 18:00 106.0 21.8081

I got output as:
Code:
010730023 2014-06-30 08:00 80.0 21.8081  1

The desired output should be:
Code:
010730023 2014-06-30 08:00 80.0 21.8081 1
010730023 2014-06-30 09:00 80.0 21.8081 1
010730023 2014-06-30 10:00 78.0 21.8081 1
010730023 2014-06-30 11:00 92.0 21.8081 1
010730023 2014-06-30 12:00 89.0 21.8081 1
010730023 2014-06-30 13:00 83.0 21.8081 1
010730023 2014-06-30 14:00 80.0 21.8081 1
010730023 2014-06-30 15:00 82.0 21.8081 1
010730023 2014-06-30 16:00 88.0 21.8081 1
010730023 2014-06-30 17:00 89.0 21.8081 1
010730023 2014-06-30 18:00 106.0 21.8081 1

# 13  
Old 04-19-2016
Hello Kathy,

Not sure why my code is not worked for you, see as follows it works well and fine for me, could you please try this one(which is same as previous posts) and let me know how it goes then.
Following is the Input_file:
Code:
cat Input_file
site Date time value1 value2
0023 2014-01-01 00:00 32.0 23.7
0023 2014-01-01 01:00 38.0 29.9
0023 2014-01-01 02:00 85.0 26.6
0023 2014-01-01 03:00 34.0 25.3
0023 2014-01-01 04:00 37.0 23.8
0023 2014-01-01 05:00 80.0 20.3
0023 2014-01-01 06:00 90.0 20.0
0023 2014-01-01 07:00 180.0 20.0
0023 2014-01-01 08:00 30.0 20.0

Then following code I have used in bash.
Code:
awk 'NR==1{
                 print;
                 next
           }
           {
                 split($3, A,":");
                 if($4/$NF=3){
                                 if(site_id==$1){
                                                 count++
                                                };
                                 if(!previous)  {
                                                 previous=A[1]
                                                };
                                 if(A[1]-previous==1){
                                                         P=P?P ORS $0 OFS count:$0 OFS count;
                                                         Q++;
                                                         previous=A[1];
                                                         site_id=$1
                                                     }
                                 else           {
                                                         previous=A[1];
                                                         site_id=$1
                                                }
                              }
                 else         {
                                 previous=A[1];
                                 P=Q=""
                              };
                 if(Q==3)     {
                                 print P;
                                 P=""
                              };
           }
    '  Input_file

Output will be as follows.
Code:
site Date time value1 value2
0023 2014-01-01 05:00 80.0 20.3 1
0023 2014-01-01 06:00 90.0 20.0 2
0023 2014-01-01 07:00 180.0 20.0 3

Thanks,
R. Singh
# 14  
Old 04-19-2016
I have to apologize: in function PRT's for loop, there's an m too many.
Code:
                                 for (i=1;m i<=LC; i++) print LN[i], CNT
                                          ^--- this one.

Don't ask me how it came there (typo?), nor why it worked with the first sample. Correction: Remove it
Code:
                                 for (i=1; i<=LC; i++) print LN[i], CNT

and try again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Forward Display from compute node

Hello, sorry if this is an easy thing, but I tried to make it run the whole morning, but it doesn't work so far... I am logged in on a visualization node of a high performance cluster. On this node I can run several gui-based software for the postprocessing of my cfd-data. As the... (1 Reply)
Discussion started by: Shlaeae
1 Replies

2. Shell Programming and Scripting

Compute in milisecond by use of mktime

Hi, I want to calculate diff b/w these starttime and endtime with use of mktime. I need response time in milisecond. I am using mktime to get these times. last three digits are in milisecond Starttime 2013-04-03 08:54:19,989 End time 2013-04-03 08:54:39,389 (9 Replies)
Discussion started by: random_thoughts
9 Replies

3. Shell Programming and Scripting

Compute Difference and Edit second, third columns

Hi Friends, My input file is like this chr1 100 200 chr1 300 330 chr1 2000 2000 chr1 5000 5000 chr2 7790 7890 chr2 8000 8000 If the difference of third and second columns is zero, then subtract 500 from second column and add 500 to the third column. So, my output would be chr1... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. Shell Programming and Scripting

remove consecutive duplicate rows

I have some data that looks like, 1 3300665.mol 3300665 5177008 102.093 2 3300665.mol 3300665 5177008 102.093 3 3294015.mol 3294015 5131552 102.114 4 3294015.mol 3294015 5131552 102.114 5 3293734.mol 3293734 5129625 104.152 6 3293734.mol ... (13 Replies)
Discussion started by: LMHmedchem
13 Replies

5. Web Development

How to compute previous and next buttons?

I have a project to migrate my club's membership database from Access to web based using MySQL/PHP, but I have a problem I can't get my head around and would appreciate some help... Background... I want to be able to display each member's data on screen and add a Previous and Next button to move... (2 Replies)
Discussion started by: JerryHone
2 Replies

6. Shell Programming and Scripting

compute compilation time using script

Hi, I use this script to compute compilation time several time to get system performance and compare different system: #!/bin/sh # measure the different between time before and # after the compilation of benchmark # Start at iteration 1 num=1 while do # Add one to the iteration... (3 Replies)
Discussion started by: zainab
3 Replies

7. Shell Programming and Scripting

to compute diskspace

Guys, have any idea for the script like this? also to compute w/ decimal. thanks a=10 b=20 c=30 d=40 if a < b then ( a -b)*1024 = free space b + (c -d) = total space if a > b then (b / d)*1024 = cpu (3 Replies)
Discussion started by: kenshinhimura
3 Replies

8. Shell Programming and Scripting

How to capture 2 consecutive rows when a condition is true ?

Hi All, i have an input below. As long as "x= 1" , i would want to capture 2 lines using sed or awk for eg : 0001 x= 1 $---------------------------------..-.--.. 0001 tt= 137 171 423 1682 2826 0 Pls help. Thanks in advance. Note that the number of lines in each block do... (37 Replies)
Discussion started by: Raynon
37 Replies

9. UNIX for Advanced & Expert Users

CPU Usage at another Compute Node?

Hi, I am trying to get the cpu usage of a job/process. The machine that has these jobs is a cluster with 1 master node and 10 compute nodes. Now, the complexity is that not all jobs are on the master node. So, in order to get the cpu usage of a job at another node, I have to ssh into it and... (0 Replies)
Discussion started by: davidfrank
0 Replies

10. Shell Programming and Scripting

compute total from a text file

Hi, I've encountered a problem with a perl and ksh script that totals a certain field in a text file. The computed total of the script is 295540304 but the expected is 297959288, a 2 million difference. The KSH script reads from bottom to top, and the discrepancy started on line 47 (1279th MAN... (1 Reply)
Discussion started by: rsf01
1 Replies
Login or Register to Ask a Question