Unix-打印不同的字段长度

时间:2018-10-19 16:12:30

标签: unix if-statement awk printing

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

列类型为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.

输入

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

1 个答案:

答案 0 :(得分:0)

根据您的伪代码,我想您想要

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

在检查数据质量的情况下,还有其他长度计数。

如果您希望计数为0而不是null,请按照注释中的建议更改为count[n]+0