已更新4
最终成功通过首先替换逗号
sed -i -e 's/,//g' ./test.txt
已更新3
gawk -v FPAT="\"$re\"|'$re'|([^,]*)" 'max2<${2/,/}{max2=${2/,/}} max3<$3{max3=$3} !(NR%48){print NR,$1,max2,max3; max2=max3=0}' ./test.txt
当定义限制器不起作用时,我尝试在主体变量内替换逗号 但错误
gawk: max2<${2/,/}{max2=${2/,/}} max3<$3{max3=$3} !(NR%48){print NR,$1,max2,max3; max2=max3=0}
gawk: ^ syntax error
gawk: max2<${2/,/}{max2=${2/,/}} max3<$3{max3=$3} !(NR%48){print NR,$1,max2,max3; max2=max3=0}
gawk: ^ syntax error
gawk: max2<${2/,/}{max2=${2/,/}} max3<$3{max3=$3} !(NR%48){print NR,$1,max2,max3; max2=max3=0}
gawk: ^ unterminated regexp
gawk: cmd. line:1: max2<${2/,/}{max2=${2/,/}} max3<$3{max3=$3} !(NR%48){print NR,$1,max2,max3; max2=max3=0}
gawk: cmd. line:1: ^ unexpected newline or end of string
updated2
re='[[:space:]]|,[[:space"]]'
gawk -v FPAT="\"$re\"|'$re'" '{print $2}' ./test.txt
我使用了这个命令,但是当打印第二列时,
以数字和逗号结尾,“5615338”,但不仅仅是数字5615338
更新 输入示例
2017/07/01_00:05 10619159, 4056079
2017/07/01_00:10 21226878, 4701043
2017/07/01_00:15 21226878, 5950847
2017/07/01_00:20 16363494, 5950847
2017/07/01_00:25 14633717, 3741938
2017/07/01_00:30 15704459, 4635459
2017/07/01_00:35 21998339, 5317282
2017/07/01_00:40 21998339, 5317282
2017/07/01_00:45 16374904, 4908459
2017/07/01_00:50 14202981, 4908459
2017/07/01_00:55 14079446, 5883581
2017/07/01_01:00 13103851, 5883581
2017/07/01_01:05 15507895, 2809263
2017/07/01_01:10 18997682, 2528104
2017/07/01_01:15 18997682, 2422419
2017/07/01_01:20 11074793, 2738467
2017/07/01_01:25 7052341, 3305620
2017/07/01_01:30 8575741, 3305620
2017/07/01_01:35 8575741, 2954594
2017/07/01_01:40 8428957, 2954594
2017/07/01_01:45 6572863, 2567163
2017/07/01_01:50 9342291, 2073510
2017/07/01_01:55 10468132, 2073510
2017/07/01_02:00 10872895, 1918255
2017/07/01_02:05 13468878, 1983474
2017/07/01_02:10 13468878, 1983474
2017/07/01_02:15 8947705, 1788545
2017/07/01_02:20 2908146, 1608482
2017/07/01_02:25 2553990, 2154687
2017/07/01_02:30 2529649, 2229552
2017/07/01_02:35 4735841, 2679731
2017/07/01_02:40 4735841, 2679731
2017/07/01_02:45 2686580, 1795024
2017/07/01_02:50 1204155, 1795024
2017/07/01_02:55 1473997, 1577228
2017/07/01_03:00 1571244, 1852244
2017/07/01_03:05 1571244, 1852244
2017/07/01_03:10 1486433, 1574531
2017/07/01_03:15 1404316, 1574531
2017/07/01_03:20 1238772, 1138933
2017/07/01_03:25 1186353, 1138933
2017/07/01_03:30 1082074, 1459387
2017/07/01_03:35 1082074, 1459387
2017/07/01_03:40 1192972, 1414292
2017/07/01_03:45 1340513, 1388572
2017/07/01_03:50 1490070, 1388572
2017/07/01_03:55 1490070, 1319763
2017/07/01_04:00 974498, 1100755
2017/07/01_04:05 1048620, 1089887
2017/07/01_04:10 1048620, 1089887
2017/07/01_04:15 653530, 981357
2017/07/01_04:20 665320, 1156669
2017/07/01_04:25 665320, 1156669
2017/07/01_04:30 791647, 1108627
2017/07/01_04:35 791647, 1204707
2017/07/01_04:40 1585158, 1204707
2017/07/01_04:45 1585158, 1234179
2017/07/01_04:50 664379, 1234179
2017/07/01_04:55 669504, 1134438
2017/07/01_05:00 1392480, 1223190
2017/07/01_05:05 1392480, 1223190
2017/07/01_05:10 810646, 1104696
2017/07/01_05:15 787808, 1008312
2017/07/01_05:20 809652, 1247696
2017/07/01_05:25 809652, 1277104
2017/07/01_05:30 776918, 1277104
2017/07/01_05:35 1216307, 1058635
2017/07/01_05:40 1216307, 1226774
2017/07/01_05:45 1628582, 1226774
2017/07/01_05:50 2695521, 1156884
2017/07/01_05:55 2695521, 1344584
2017/07/01_06:00 2732553, 1344584
2017/07/01_06:05 2794176, 2654515
2017/07/01_06:10 2794176, 2654515
2017/07/01_06:15 2580823, 2578247
2017/07/01_06:20 3342221, 2373548
2017/07/01_06:25 6095447, 2501782
2017/07/01_06:30 6095447, 3032341
2017/07/01_06:35 2642484, 3280565
2017/07/01_06:40 3445436, 3310250
2017/07/01_06:45 5505926, 3310250
2017/07/01_06:50 5505926, 2981384
2017/07/01_06:55 6179556, 2498714
2017/07/01_07:00 6179556, 2498714
2017/07/01_07:05 5862156, 2599012
2017/07/01_07:10 5792697, 2599012
2017/07/01_07:15 7817501, 3143155
2017/07/01_07:20 7817501, 3143155
2017/07/01_07:25 6658178, 3056959
2017/07/01_07:30 6658178, 2944126
2017/07/01_07:35 5871045, 3088451
2017/07/01_07:40 5871045, 3266807
2017/07/01_07:45 5607171, 3332246
2017/07/01_07:50 4876884, 3772675
2017/07/01_07:55 6247666, 3772675
2017/07/01_08:00 6247666, 3671781
2017/07/01_08:05 5615338, 3558779
预期输出
2017/07/01_04:00 21998339 5950847
2017/07/01_08:00 7817501 3772675
如何使用一行来计算每行数的最大值
每隔5分钟将MRTG数据更改为每4小时数据,每48行使用最大值计算价格
我在下面写,但最大值是不正确的,我怀疑awk看到值为字符串,然后我添加+ 1-1将其转换为数字,但仍然是错误的,哪里错了?
awk 'NR==1 {sum2max=$2+1-1;sum3max=$3+1-1;} (NR>1 && sum2max<$2) {sum2max=$2+1-1;} NR>1 && sum3max<$3 {sum3max=$3+1-1;} (NR%48)==0 {print NR,$1,sum2max,sum3max;sum2max=0; sum3max=0;}'
答案 0 :(得分:1)
你可以稍微简化代码
$ awk 'max2<$2{max2=$2}
max3<$3{max3=$3}
!(NR%48){print NR,$1,max2,max3; max2=max3=0}' file
由于您可以将最大值重置为零,因此可以删除初始化NR==1
,同时+1-1
似乎是多余的,awk
应该能够正确地转换数字,如果没有+0
就足够了。