0

我想打印每个长度在字段中出现的总次数。

列类型为 varChar,该字段中的字符串长度为 9、10 或 15 个字符。我想知道每个长度有多少个。

我的代码:

awk -F'|' 
'NR>1 $61!="" && 
if /length($61)=15/ then {a++} 
elif /length($61)=10/ then {b++} 
else /length($61)=9/ then {c++} 
fi {print a ", " b ", " c}'

错误:

awk -F'|' 'NR>1 $61!="" && if /length($61)=15/ then {a++} elif /length($61)=10/ then {b++} else /length($61)=9/ then {c++} fi {print a ", " b ", " c}' 
 Syntax Error The source line is 1.
 The error context is
                NR>1 >>>  $61!= <<<
 awk: 0602-500 Quitting The source line is 1.

输入

具有 120 万行和第 61 列的管道分隔 .sqf 文件是 varChar 15。

4

1 回答 1

0

根据你的伪代码我猜你想要

awk -F'|' -v OFS=', ' 'NR>1 {count[length($61)]++}
                       END  {print count[15],count[10],count[9]}' file

在进行数据质量检查时,您还将在那里计算其他长度。

如果您想让 0 而不是 null 缺失计数,请count[n]+0按照评论中的建议更改为。

于 2018-10-19T19:11:04.057 回答