不确定为什么,但是awk将我的变量 x 舍入为2dp或正则表达式模式以测试整数是否不能正常工作。
我的代码:
awk 'BEGIN{for (i=972286200; i<=972384300; i=i+900) {x=((i/86400)-0.375); if(x ~ /^[0-9]+$/ ) {s=i} printf("%010d,%.3f,%010d\n",i,x,s)}}'
代码正在尝试创建具有如下列的数据集:
当前产生:
0972286200,11252.938,0000000000
0972287100,11252.948,0000000000
0972288000,11252.958,0972288000
0972288900,11252.969,0972288900
0972289800,11252.979,0972289800
0972290700,11252.990,0972290700
0972291600,11253.000,0972291600
0972292500,11253.010,0972292500
0972293400,11253.021,0972293400
0972294300,11253.031,0972294300
0972295200,11253.042,0972295200
0972296100,11253.052,0972295200
0972297000,11253.062,0972295200
0972297900,11253.073,0972295200
...
...
0972372600,11253.938,0972295200
0972373500,11253.948,0972295200
0972374400,11253.958,0972374400
0972375300,11253.969,0972375300
0972376200,11253.979,0972376200
0972377100,11253.990,0972377100
0972378000,11254.000,0972378000
0972378900,11254.010,0972378900
0972379800,11254.021,0972379800
0972380700,11254.031,0972380700
0972381600,11254.042,0972381600
0972382500,11254.052,0972381600
0972383400,11254.062,0972381600
0972384300,11254.073,0972381600
我希望它产生:
0972286200,11252.938,0000000000
0972287100,11252.948,0000000000
0972288000,11252.958,0000000000
0972288900,11252.969,0000000000
0972289800,11252.979,0000000000
0972290700,11252.990,0000000000
0972291600,11253.000,0972291600
0972292500,11253.010,0972291600
0972293400,11253.021,0972291600
0972294300,11253.031,0972291600
0972295200,11253.042,0972291600
0972296100,11253.052,0972291600
0972297000,11253.062,0972291600
0972297900,11253.073,0972291600
...
...
0972372600,11253.938,0972291600
0972373500,11253.948,0972291600
0972374400,11253.958,0972291600
0972375300,11253.969,0972291600
0972376200,11253.979,0972291600
0972377100,11253.990,0972291600
0972378000,11254.000,0972378000
0972378900,11254.010,0972378000
0972379800,11254.021,0972378000
0972380700,11254.031,0972378000
0972381600,11254.042,0972378000
0972382500,11254.052,0972378000
0972383400,11254.062,0972378000
0972384300,11254.073,0972378000
答案 0 :(得分:1)
改为使用x==int(x)
进行int测试。
$ awk 'BEGIN {for(i=972286200; i<=972384300; i+=900)
{x=(i/86400)-0.375;
if(x==int(x)) s=i;
printf "%010d,%.3f,%010d\n",i,x,s}}'
还改变了一些小事。