我有 3 个文件,其中包含 8 行数值和文本。我试图取所有三个文件中每一行的平均值,并用这些平均值打印一个新文件。下面是三个示例文件,都是同名格式 testfile1.1, testfile1.2, testfile1.3
测试文件1.1
1
2048
8
5
5
4
9
Lat:1
测试文件1.2
1
2048
10
7
7
4
9
Lat:1
测试文件1.3
1
2048
3
6
3
4
6
Lat:7
我希望输出文件如下所示(取平均值后)
平均文件1
1
2048
7
6
5
4
8
Lat:3
希望这对我正在尝试做的事情有意义!
我尝试过使用 awk、sed 的不同组合,它们对 3-4 行数据运行良好,但我的实际数据在 40 多个文件名中有 2000 多行
编辑:所以我能够理解如何控制我想要打印的 sig figs 以及如何编辑正则表达式以更好地匹配浮动小数。
(请让我知道我是否应该将此作为另一个问题并删除此问题!)。
我的实际数据有很多其他行,其中包含文本以及我想要取平均值的值。我试图创建额外的字符串,但后来变得更加困惑。在我的真实文件中,在某些行上,我想要不同的命令,例如从行中打印文本,对实际数据取平均值,复制文本的行和数据的平均值以及日期和时间的平均值。
下面是 2 个文件的副本(每行都有我想对它们做的评论)。
豆腐1.1
ABCDEFGH #print text into output file (same on both files)
1 # Take average of values across all the files in this line
2048 # Take average of values across all the files in this line
8 # Take average of values across all the files in this line
5 # Take average of values across all the files in this line
5 # Take average of values across all the files in this line
4 # Take average of values across all the files in this line
9.5 # Take average of values across all the files in this line
1 # Take average of values across all the files in this line
90.00 # Check and make sure value in this line across print if same
Sprite # check and see if text is same across all values and print if same
cats10 # check and see if text is same across all values and print if same
07/02/20 # See below for explantion on next 3 lines
08:32
08:32
290.000000 # average across all 3 files on this line
10.750000 # average across all 3 files on this line
SCANS23 # output should be SCANS "average of values"
INT_TIME57500 # output should be INT_TIME with sum of all values
SITE northpole #Check if all lines are same if so print line
LONGITUDE -147.850037 # Output should be LONGITUDE%f
LATITUDE 64.859375 # Output should be LONGITUDE%f
第 13 行是数据的来源日期,第 14 行是开始时间和结束时间。可能使用某种日期到十进制命令..有没有办法取日期的平均值?如果一个数据是在 2020 年 7 月 2 日获取的,而另一个数据是在 2018 年 7 月 2 日获取的,那么输出可以是 19 年 7 月 2 日吗?时间的平均值也会被考虑在内。
豆腐1.2
ABCDEFGH #print text into output file (same on both files)
1 # Take average of values across all the files in this line
2048 # Take average of values across all the files in this line
10 # Take average of values across all the files in this line
7 # Take average of values across all the files in this line
7 # Take average of values across all the files in this line
4 # Take average of values across all the files in this line
8 # Take average of values across all the files in this line
1 # Take average of values across all the files in this line
90.00 # Check and make sure value in this line across print if same
Sprite # check and see if text is same across all values and print if same
cats10 # check and see if text is same across all values and print if same
07/02/20 # See below for explanation on next 3 lines
08:32
08:32
290.000000 # average across all 3 files on this line
10.750000 # average across all 3 files on this line
SCANS23 # output should be SCANS "average of values"
INT_TIME57500 # output should be INT_TIME with sum of all values
SITE northpole #Check if all lines are same if so print line
LONGITUDE -147.850037 # Output should be LONGITUDE%f
LATITUDE 64.859375 # Output should be LONGITUDE%f
我厌倦了尝试在脚本中包含多个字符串起始值的尝试,但很快就变得非常混乱。
awk -F: '
FNR==1 { c++ };
/^LATITUDE/ { a[FNR] += $6 };
/^SCANS/ { a[FNR] += $2 };
/^[+-]?([0-9]*[.])?[0-9]+$/ { a[FNR] += $1 };
END {
for (i in a) {
printf (i==22 ? "LATITUDE%f": i==18 ? "SCANS%2.3f": "%f") "\n", a[i] / c
}
}' tofu1.* > askforhelp
这给了我
$ more askforhelp
90.000000
LATITUDE0.000000
290.000000
10.750000
SCANS0.000
1.000000
2048.000000
6.333333
4.666667
5.000000
4.000000
7.833333
2.666667
我还尝试一次添加多个文本字符串,当我完全没有从这次尝试中得到输出时,我感到非常困惑。
awk -F: '
FNR==1 { c++ };
/^LATITUDE/ { a[FNR] += $6 };
/^LONGITUDE/ { a[FNR] += $5 };
/^SITE/ { a[FNR] += $4 };
/^INT_TIME/ { a[FNR] += $3 };
/^SCANS/ { a[FNR] += $2 };
/^[+-]?([0-9]*[.])?[0-9]+$/ { a[FNR] += $1 };
END {
for (i in a) {
printf (i==22 ? "LATITUDE%f":
i==21 ? "LONGITUDE%2.3f":
i==20 ? "SITE%2.3f":
i==19 ? "INT_TIME%2.3f":
i==18 ? "SCANS%2.3f": "%f") "\n", a[i] / c
}
}' /home/lmdjeu/test/test1.* > /home/lmdjeu/test/askforhelp