Sample Header Ad - 728x90

Complete file line by line reading dates

0 votes
2 answers
300 views
I have a text file containing 2 columns. The first one has DATES (DD/MM/YYYY) and the second one, numbers. It looks like this:
15/01/1945 105.0
16/01/1945   4.2
17/01/1945   3.0
31/01/1945  12.0
01/02/1945   3.0
02/02/1945 125.0
05/02/1945   0.3
And I need to fill the file with this conditions: 1. First date 01/01/1945 2. Last date 31/12/2021 3. Dates must be consecutive, with a difference of one day between lines 4. If there is a missing date, we have to complete the line with the correct date and the number -99.0 So, the final file should look like this:
01/01/1945 -99.0
02/01/1945 -99.0
03/01/1945 -99.0
04/01/1945 -99.0
05/01/1945 -99.0
06/01/1945 -99.0
07/01/1945 -99.0
08/01/1945 -99.0
09/01/1945 -99.0
10/01/1945 -99.0
11/01/1945 -99.0
12/01/1945 -99.0
13/01/1945 -99.0
14/01/1945 -99.0
15/01/1945 105.0
16/01/1945   4.2
17/01/1945   3.0
18/01/1945 -99.0
19/01/1945 -99.0
20/01/1945 -99.0
21/01/1945 -99.0
22/01/1945 -99.0
23/01/1945 -99.0
24/01/1945 -99.0
25/01/1945 -99.0
26/01/1945 -99.0
27/01/1945 -99.0
28/01/1945 -99.0
29/01/1945 -99.0
30/01/1945 -99.0
31/01/1945  12.0
01/02/1945   3.0
02/02/1945 125.0
03/02/1945 -99.0
04/02/1945 -99.0
05/02/1945   0.3
06/02/1945 -99.0
07/02/1945 -99.0
...
30/12/2021 -99.0
31/12/2021 -99.0
I have tried by using a fortran program but it doesn't work. I think that maybe using awk or sed or both. This is what I get when I read Ed's script:
`        
meteo@poniente:/datos$ cat awk.script
#!/bin/bash
cat tst.awk
awk { dates2vals[$1] = $2 }
END {
    begDate = "01/01/1945"
    endDate = "31/12/2000"
    begSecs = mktime(gensub("(.*)/(.*)/(.*)","\\3 \\2 \\1 12 00 00",1,begDate))
    daySecs = 24 * 60 * 60
    for (curSecs=begSecs; curDate!=endDate; curSecs+=daySecs) {
        curDate = strftime("%d/%m/%Y",curSecs)
        print curDate, (curDate in dates2vals ? dates2vals[curDate] : "-99.0")
    }
}
` And this is what I get when I run Ed's script:
`
meteo@poniente:/datos$ ./tst.awk
01/01/1946   3.0
02/01/1946  14.2
...
14/11/2021   0.0
15/11/2021   0.0
16/11/2021   0.0
17/11/2021   0.0
18/11/2021   0.0
19/11/2021   0.0
20/11/2021   0.0
21/11/2021   0.0
22/11/2021  54.1
23/11/2021 -99.0
24/11/2021  27.4
25/11/2021   0.0
29/11/2021   0.0
30/11/2021   0.0
awk: li­ne ord.:1: {
awk: line ord.:1:  ^ unexpected newline or end of string
./awk.script: li­ne 4: END: command not found
./awk.script: li­ne 5: begDate: command not found
./awk.script: li­ne 6: endDate: command not found
./awk.script: li­ne 7: syntax error near unexpected element `('
./awk.script: li­ne 7: `    begSecs = mktime(gensub("(.*)/(.*)/(.*)","\\3 \\2 \\1 12 00 00",1,begDate))'
meteo@poniente:/datos$
`
Asked by David (1 rep)
Feb 11, 2022, 11:52 AM
Last activity: Feb 17, 2022, 02:37 PM