如何基于单个键列合并两个文件并自动修复固定列并填充缺失数据

如何基于单个键列合并两个文件并自动修复固定列并填充缺失数据

我有两组文件试图合并成一个文件并填充关联的缺失数据。

文件都是,分隔的

第一个文件包含 13 列,其中第 8 列包含格式中的日期YYYY-MM-DD(注意:该文件缺少 44 天。第二个文件有 2 列,第一个文件的格式为完整日历年(闰年 366 天)YYYY-MM-DD,而第二个是关联的儒略日期值。

缺少天数的示例文件 #1

06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-26,Parts-per-million,24,100.0,0.379167,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-27,Parts-per-million,24,100.0,0.2875,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-28,Parts-per-million,11,46.0,0.163636,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-30,Parts-per-million,20,83.0,0.23,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-31,Parts-per-million,24,100.0,0.195833,10

我尝试使用以下命令将文件合并在一起并创建一个包含 14 列的新文件,因为缺少的日期已被填充并添加了儒略日期。我还寻找代码来自动填充初始文件中第 1-7 列和第 9 列的固定值,并在-999缺少此数据的情况下填充第 10-13 列。

awk -F ',' 'NR==FNR {h[$1] = $14; next} {print $1,$2,$3,$4,$5,$6,$7,$8,h[$2],$9,$10,$11,$12,$13}' temp2.tmp temp1.tmp  > temp3.tmp

06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-26,26,Parts-per-million,24,100.0,0.379167,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-27,27,Parts-per-million,24,100.0,0.2875,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-28,28,Parts-per-million,11,46.0,0.163636,10
**06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-29,29,Parts-per-million,-999,-999,-999,-999**
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-30,30,Parts-per-million,20,83.0,0.23,10
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-31,31,Parts-per-million,24,100.0,0.195833,10

答案1

现在,日期时间计算始终是一件......困难的事情。特别是。如果日期时间序列跨越午夜、月末或年末,或夏令时切换。在这里,为了安全起见,我们使用纪元秒。使用该命令转换回日期时间date可能不适用于所有 *nix 版本。并且,我们将TZ变量设置为“UTC”以避免 DST 问题。尝试不使用,你就会看到。来吧,尝试一下:

export TZ=UTC                                                       # get rid of side effects, e.g. DST switching

cut -d, -f8 samplefile | date -f- +%s | paste - samplefile > TMP1   # prepend epoch seconds to the input file

{ read MIN DUMMY                                                    # get file´s MIN and MAX dates
  while read TMP DUMMY           
     do MAX=$TMP
     done                                                           # and calculate a sequence of days between them
     eval echo @{$MIN..$MAX..86400} | tr ' ' $'\n' | date -f- +$'%s\t%Y-%m-%d\t%y%j'
} < TMP1 > TMP2                                                     # in epoch, yyyy-mm-dd, and julian format

join -a1 -a2 -- TMP1 TMP2 | awk -F"[, ]" '                          # join first and second intermediate files
NF == 3         {split($0, TMPINS)                                  # orig. files line missing; fill temp array with epoch etc. data
                 $0  = SAVED                                        # get last saved complete line
                 $9  = TMPINS[2]                                    # overwrite "yesterday´s" date 
                 $NF = TMPINS[3]                                    # append julian date
                 $11 = $12 = $13 = $14 = -999                       # set invalid indicator
                }
NF >= 13        {SAVED = $0                                         # correct line? save it
                 $1 = $1                                            # recreate line with OFS char
                }

                {sub($1",",_)                                       # for all lines: remove leading epoch field
                 $14 = $15                                          # put julian date into right place
                 NF--                                               # get rid of last field; may not work in ALL awks
                }
1                                                                   # default action: print
' OFS=","
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-26,Parts-per-million,24,100.0,0.379167,10,20026
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-27,Parts-per-million,24,100.0,0.2875,10,20027
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-28,Parts-per-million,11,46.0,0.163636,10,20028
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-29,Parts-per-million,-999,-999,-999,-999,20029
06,037,0016,42101,34.14435,-117.85036,1-HOUR,2020-01-30,Parts-per-million,20,83.0,0.23,10,20030

在 FIFO 的帮助下,整个事情可以写在一个长长的命令管道上:

mkfifo TMPFIFO
cut -d, -f8 samplefile | date -f- +%s | tee -a >(read MIN; while read TMP; do MAX=$TMP; done; eval echo @{$MIN..$MAX..86400} | tr ' ' $'\n' > TMPFIFO) | paste - samplefile | join -a1 -a2 -- - <(date -fTMPFIFO +$'%s\t%Y-%m-%d\t%y%j') | awk -F"[, ]" 'NF == 3 {split($0, TMPINS); $0 = SAVED; $9 = TMPINS[2]; $NF = TMPINS[3]; $11 = $12 = $13 = $14 = -999} NF >= 13 {SAVED = $0; $1 = $1} {sub($1",",_); $14 = $15; NF--} 1' OFS=","

相关内容