Insert rows with computations of next row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert rows with computations of next row
# 1  
Old 11-24-2007
Insert rows with computations of next row

Hello folks,
I have data collected in every 3 hours. But, I would like to expand this to 1 hour interval by equally dividing with next row.
For example, I want to keep the first value 1987-01-01-00z 2.0, but following all record should be re-written as follow.
1987-01-01-03z 5.0 becomes 1.66667 after dividing by 3, then two new records 1987-01-01-01z 1.66667 and 1987-01-01-02z 1.66667 should be inserted, and continue...
Also, all precision should be same.
Thanks in advance.

Jae

<input>
1987-01-01-00Z 2.0
1987-01-01-03Z 5.0
1987-01-01-06Z 10.0
1987-01-01-09Z 120.0
1987-01-01-12Z 10
1987-01-01-15Z 8
1987-01-01-18Z 28.302
1987-01-01-21Z 282.566
1987-01-02-00Z 0
1987-01-02-03Z 2.0

<output>
1987-01-01-00Z 2.00000
1987-01-01-01Z 1.66667
1987-01-01-02Z 1.66667
1987-01-01-03Z 1.66667
1987-01-01-04Z 3.33333
1987-01-01-05Z 3.33333
1987-01-01-06Z 3.33333
1987-01-01-07Z 40.0000
1987-01-01-08Z 40.0000
1987-01-01-09Z 40.0000
1987-01-01-10Z 3.33333
1987-01-01-11Z 3.33333
1987-01-01-12Z 3.33333
1987-01-01-13Z 2.66667
1987-01-01-14Z 2.66667
1987-01-01-15Z 2.66667
1987-01-01-16Z 9.43400
1987-01-01-17Z 9.43400
1987-01-01-18Z 9.43400
1987-01-01-19Z 94.1887
1987-01-01-20Z 94.1887
1987-01-01-21Z 94.1887
1987-01-01-22Z 0.00000
1987-01-01-23Z 0.00000
1987-01-02-00Z 0.00000
1987-01-02-01Z 0.66667
1987-01-02-02Z 0.66667
1987-01-02-03Z 0.66667

Last edited by Jae; 11-26-2007 at 01:57 PM..
# 2  
Old 11-24-2007
Wow, they certainly do have creative word-problems at Devry nowadays. Smilie

I think you want to use awk. It can read each line, separate the fields, do the division, and print with the desired precision. Should be easy.
# 3  
Old 11-25-2007
Not easy to me at all

Yes, I know awk would be the solution. But, my trial doesn't work.
Any input would be appreciated. I am noting to do with Devry.

awk '
BEGIN {
FS="-|" "";
}
{
if (NR==1) print $0
else {
getline record;
n = split( record, arr, " ")
print $1"-"$2"-"$3"-"$4-2" "arr[2]/3;
print $1"-"$2"-"$3"-"$4-1" "arr[2]/3;
print $1"-"$2"-"$3"-"$4;
}

}
' inputfile
# 4  
Old 11-25-2007
This should do the job:

Code:
awk '
NR==1{printf("%s %.5f\n", $1,$2); next}
dat!=substr($1,1,10){dat=substr($1,1,10);cnt=1}
{
  for(i=1;i<=3;i++) {
    printf "%s%02dZ %."6-length(int($2/3))"f\n",substr($1,1,11),cnt,$2/3
    cnt++
  }
}
' inputfile

Regards
# 5  
Old 11-25-2007
Thanks Franklin,

I almost got it.
but, the result after 1987-01-01-21Z using your script is not what I want (look at the hour increment).
These are:
1987-01-01-21Z 94.1887
1987-01-02-01Z 0.00000
1987-01-02-02Z 0.00000
1987-01-02-03Z 0.00000
1987-01-02-04Z 0.66667
1987-01-02-05Z 0.66667
1987-01-02-06Z 0.66667

Should be:
1987-01-01-21Z 94.1887
1987-01-01-22Z 0.00000
1987-01-01-23Z 0.00000
1987-01-02-00Z 0.00000
1987-01-02-01Z 0.66667
1987-01-02-02Z 0.66667
1987-01-02-03Z 0.66667

Last edited by Jae; 11-25-2007 at 05:24 PM..
# 6  
Old 11-25-2007
Thanks Franklin,

I almost got it.
but, the result after 1987-01-01-21Z using your script is not what I want (look at the hour increment).
These are:
1987-01-01-21Z 94.1887
1987-01-02-01Z 0.00000
1987-01-02-02Z 0.00000
1987-01-02-03Z 0.00000
1987-01-02-04Z 0.66667
1987-01-02-05Z 0.66667
1987-01-02-06Z 0.66667

Should be:
1987-01-01-21Z 94.1887
1987-01-01-22Z 0.00000
1987-01-01-23Z 0.00000
1987-01-02-00Z 0.00000
1987-01-02-01Z 0.66667
1987-01-02-02Z 0.66667
1987-01-02-03Z 0.66667

Last edited by Jae; 11-25-2007 at 05:24 PM..
# 7  
Old 11-25-2007
Check your input and output careful, if the output is wrong, I can't understand the logic.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. UNIX for Dummies Questions & Answers

Insert row into empty file...how?

Greetings: I generate an empty flat file just fine when there's no data returned from my process, as the customer wants one always (using the 1st line of the below script). However, they also want at least the column names in this flat file (row 1, the only row to be in the emply file). I'm... (7 Replies)
Discussion started by: Benrosa
7 Replies

3. Shell Programming and Scripting

Print various rows in one row

I have this in a file 11.22.33.44 yyyyyyuser With awk/sed, I need this to be output as follows alias server.domain.com='ssh yyyyyyuser@11.22.33.44' (4 Replies)
Discussion started by: anil510
4 Replies

4. UNIX for Dummies Questions & Answers

Insert rows with some rules??

Hi, I am rather new to Unix/Linus. I have this problem that I would like to solve using unix. Here is what I have start stop expression 1 5 15 2 6 10 I want a output like this position expression 1 15 2 25 3 ... (1 Reply)
Discussion started by: wanghlv
1 Replies

5. UNIX for Advanced & Expert Users

Converting rows to a single row

Hi all I have a file as below : Development System User Production i want to convert the file to below format: "Development","System","User","Production" Is it possible with UNIX ? if so can you please give me some direction on it ? Thanks, Satya Use code tags please, ty. (10 Replies)
Discussion started by: satyaranjon
10 Replies

6. UNIX for Advanced & Expert Users

convert rows to single row

Hi I want to convert multiple rows ro single row ,I have tried with below one but I am not getting what I am expecting.Please any idea a.txt conn1=stg conn2=dev path=\xxx\a1.txt fre=a conn1=stg conn2=dev path=\xxx\a2.txt freq=a awk '/a/{ORS=" "}{print}END{print "\n"}'... (5 Replies)
Discussion started by: akil
5 Replies

7. Shell Programming and Scripting

insert txt in the row

hi i am having text file like this 444 raju 666 ranga Clerk 999 rani officer 111 juhi i want to get the out put as 444 raju NA 666 ranga Clerk 999 rani officer 111 juhi NA pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

8. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

9. UNIX for Dummies Questions & Answers

insert header row into .xls

Hello, I am building an .xls file extracting info from a DB to be eventually emailed. All is good except how do I put in a header row.. like date, name of report etc. before the columns with the actual column name and data? Thanks for any assistance.. the below is after I have signed into... (11 Replies)
Discussion started by: Tish
11 Replies

10. Shell Programming and Scripting

Concatenate 2 rows into 1 row

I need to search a file for two values (valueA & valueB). ValueA will be on a different row than valueB, and concatenate the two together on the same row of my output. Example: search input file for strings "node" and "OS", combine the two results into one row input node A text text OS... (4 Replies)
Discussion started by: indianadoug
4 Replies
Login or Register to Ask a Question