我有一个正在计算自然对数 (LN) 的大型数据集。然而,在很多情况下,存在负值,并且 awk 会为每次出现的情况踢出警告行,并且不会打印新字段。我已经知道如何计算和添加附加字段。但是,我不确定如何让 awk 打印正数据点的值,并打印某种缺失的数据标识符,例如数据为负值的 NR。在这种情况下,数据将为 $11,(LN) 输出将以 $12 打印。谢谢
QUEENS_DEC,5/26/2002,3.06, ,16.61342593,0.830833333,20020526,0,50.5414,0.0391322,50.5805,**3.9236**
QUEENS_DEC,5/27/2002, ,0.8150,19.73148148,0.812083333,20020527,0.0353628,60.0271,0.0382491,60.1007,**4.0960**
QUEENS_DEC,5/28/2002, , ,19.15046296,0.77,20020528,0,58.2595,0.036267,58.2958,**4.0655**
QUEENS_DEC,5/29/2002,1.83, ,21.76388889,0.783333333,20020529,0,66.2101,0.036895,-66.247,**NR**
答案1
你可以使用一个if
测试。
例如:
awk -F, '
BEGIN { OFS="," }
{ if ($11<=0) { $12="NR" } else { $12=log($11) } ; print }
'
这会将字段 12 设置为字符串“NR”或字段 11 的对数,具体取决于看到的值。
使用您的输入文件,输出现在如下所示:
QUEENS_DEC,5/26/2002,3.06, ,16.61342593,0.830833333,20020526,0,50.5414,0.0391322,50.5805,3.92357
QUEENS_DEC,5/27/2002, ,0.8150,19.73148148,0.812083333,20020527,0.0353628,60.0271,0.0382491,60.1007,4.09602
QUEENS_DEC,5/28/2002, , ,19.15046296,0.77,20020528,0,58.2595,0.036267,58.2958,4.06553
QUEENS_DEC,5/29/2002,1.83, ,21.76388889,0.783333333,20020529,0,66.2101,0.036895,-66.247,NR