Making some wild guesses about what you really want on the 1st three lines in each of your output files (.e., all of the data with <comma>s changed to <space>s instead of throwing away the call signs), the following seems to work using ksh
on OS X to calculate the Julian dates for the 1st day of the month for the year and month specified by the filename:
#!/bin/ksh
for file in [A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9]0[01][0-9]
do m=${file#????????}
y=${file#???}
y=${y%???}
jd1=$(printf '%(%j)T' "$m/01/$y")
printf '%s,%s\n' "$file" "$jd1"
done | awk -F ' *, *' '
FNR == NR {
jdb[ARGV[ARGC++] = $1] = $2 - 1
next
}
FNR == 1 {
if(ofn) close(ofn)
ofn = FILENAME ".txt"
gsub(/,/, " ")
print > ofn
next
}
FNR == 2 {
gsub(/,/, " ")
print > ofn
next
}
FNR == 3 {
printf("Julian %s %s\n", $2, $9) > ofn
fmt = sprintf(" %%03d %%4.4s %%%d.%ds\n", length($9), length($9))
next
}
{ printf(fmt, $1 + jdb[FILENAME], $2, $9 ? $9 : "-") > ofn
}' -
With the following input files:
File: CPK2004001
Month:, 01/2004
Station Name:, "CENTRAL PARK" Call Sign:, NYC
Day,Time,StationType,Maint Indic,SkyConditions,Visibility,Weather Type,Dry Bulb Faren,Dry Bulb Cel,Wet Bulb Faren,Wet Bulb Cel,Dew Point Faren,Dew Point Cel,Rel Humd,Wind Speed,Wind Dir,Wind Char Gusts,Val. for Wind Char,Station Pressure,Press Tend,Sea Level Pressure,Report Type,Precip Total
01,0051,AO2 ,-,CLR ,10SM ,-,41 , 5.0,-, 2.1,28 , -2.2, 60 , 6 ,VRB,-,0 ,29.92,5,180,AA,-
01,0151,AO2 ,-,CLR ,10SM ,-,41 , 5.0,-, 2.2,28 , -2.2,
File: JFK2003003
Month:, 03/2003
Station Name:, "JFK AIRPORT" Call Sign:, JFK
Day,Time,StationType,Maint Indic,SkyConditions,Visibility,Weather Type,Dry Bulb Faren,Dry Bulb Cel,Wet Bulb Faren,Wet Bulb Cel,Dew Point Faren,Dew Point Cel,Rel Humd,Wind Speed,Wind Dir,Wind Char Gusts,Val. for Wind Char,Station Pressure,Press Tend,Sea Level Pressure,Report Type,Precip Total
01,0051,AO2 ,-,CLR ,10SM ,-,41 , ,-, 2.1,28 , -2.2, 60 , 6 ,VRB,-,0 ,29.92,5,180,AA,-
10,0151,AO2 ,-,CLR ,10SM ,-,41 , ,-, 2.2,28 , -2.2,
File: TEB2000012
Month:, 12/2000
Station Name:, "TETERBORO AIRPORT" Call Sign:, TEB
Day,Time,StationType,Maint Indic,SkyConditions,Visibility,Weather Type,Dry Bulb Faren,Dry Bulb Cel,Wet Bulb Faren,Wet Bulb Cel,Dew Point Faren,Dew Point Cel,Rel Humd,Wind Speed,Wind Dir,Wind Char Gusts,Val. for Wind Char,Station Pressure,Press Tend,Sea Level Pressure,Report Type,Precip Total
01,0051,AO2 ,-,CLR ,10SM ,-,41 , 5.0,-, 2.1,28 , -2.2, 60 , 6 ,VRB,-,0 ,29.92,5,180,AA,-
31,0151,AO2 ,-,CLR ,10SM ,-,41 , -5.0,-, 2.2,28 , -2.2,
it produces the following output files:
File: CPK2004001.txt
Month: 01/2004
Station Name: "CENTRAL PARK" Call Sign: NYC
Julian Time Dry Bulb Cel
001 0051 5.0
001 0151 5.0
File: JFK2003003.txt
Month: 03/2003
Station Name: "JFK AIRPORT" Call Sign: JFK
Julian Time Dry Bulb Cel
060 0051 -
069 0151 -
File: TEB2000012.txt
Month: 12/2000
Station Name: "TETERBORO AIRPORT" Call Sign: TEB
Julian Time Dry Bulb Cel
336 0051 5.0
366 0151 -5.0
Note that this code only works with a 1993 or later version of the Korn shell. (OS X comes with a BSD-based date
utility; not a GNU date
utility. And, although BSD date
has a -d
option, it sets the kernel's idea of daylight savings time offsets; it does not have a way to use the GNU date -d
option to specify an alternative date to process.