这是 gawk 4.0.0,在带有 cygwin 的 Windows 7 上运行。该程序的调用方式如下
gawk -f procjournal.gawk testdata
我有一些看起来像这样的数据:
"Date";"Type";"Amount";"Balance"
"6/11/2013 11:51:17 AM";"Transaction Tax";-427.5;399313884.46
"6/11/2013 11:51:17 AM";"Market Transaction";47500;399314311.96
"6/11/2013 11:12:42 AM";"Transaction Tax";-549.92;399266811.96
"6/11/2013 11:12:42 AM";"Market Transaction";61101.78;399267361.88
我想提取交易行,从第一个字段中删除日期部分,并将时间戳重新格式化为小数。我想我可以用这个 awk 程序做到这一点:
FS=";"
OFS=";"
/Market Transaction/ {
split($1, itemdate, " ");
tmp = itemdate[2];
split(tmp, hms, ":");
timestamp = hms[3] + (hms[2] * 60) + (hms[1] * 3600);
if (itemdate[3] == "AM")
timestamp += 12 * 3600;
timestamp /= 3600.0;
$1 = timestamp;
print;
}
但我的输出如下所示:
"Date";"Type";"Amount";"Balance"
"Date";"Type";"Amount";"Balance"
"6/11/2013 11:51:17 AM";"Transaction Tax";-427.5;399313884.46
"6/11/2013 11:51:17 AM";"Transaction Tax";-427.5;399313884.46
"6/11/2013 11:51:17 AM";"Market Transaction";47500;399314311.96
"6/11/2013 11:51:17 AM";"Market Transaction";47500;399314311.96
11.8547;"Market Transaction";47500;399314311.96
"6/11/2013 11:12:42 AM";"Transaction Tax";-549.92;399266811.96
"6/11/2013 11:12:42 AM";"Transaction Tax";-549.92;399266811.96
"6/11/2013 11:12:42 AM";"Market Transaction";61101.78;399267361.88
"6/11/2013 11:12:42 AM";"Market Transaction";61101.78;399267361.88
11.2117;"Market Transaction";61101.78;399267361.88
为什么要打印不匹配的行,我该如何抑制它?