[英]How to use AWK Using with IF Condition
I need to use AWK command to find count of delimiters in a file and if the count is more than expected then I need to captuure the records. 我需要使用AWK命令在文件中查找分隔符的数量,如果该数量超过预期,则需要捕获记录。
Code used: 使用的代码:
awk 'BEGIN { FS= "^A"; if ( 31 < NF-1) print $0 }' file.dat
but it is not working. 但它不起作用。
As fedorqui comments, you don't have any data in a BEGIN block. 作为fedorqui的注释,BEGIN块中没有任何数据。 You have to read at least 1 record to determine how many fields you have:
您必须至少读取1条记录才能确定您拥有多少个字段:
awk -F"^A" 'NR==1 && NF >= 32 {exit} {print}' file.dat
Assuming that "^A"
is a string of two characters 假设
"^A"
是两个字符的字符串
awk 'BEGIN { FS= "\\^A"} NF-1 > 32' file.dat
will do, as {print}
is the default action for a match and the two most common instances of awk (gawk and mawk) both translate "\\\\^A"
to a literal caret followed by the letter A
. 会这样做,因为
{print}
是匹配的默认操作,并且两个最常见的awk实例(gawk和mawk)都将"\\\\^A"
转换为文字插入符,后跟字母A
On the other hand if "^A"
means Control-A
you must properly quote the control character when you type the line at the shell prompt (eg, in bash+readline it is Control-V
Control-A
) and you must not use the backslashes. 另一方面,如果
"^A"
表示Control-A
,则在shell提示符下键入该行时必须正确引用控制字符(例如,在bash + readline中为Control-V
Control-A
),并且不得使用反斜杠。
A slightly shorter version: 稍短的版本:
awk _F"^A" 'NF > 32' file.dat
Your original construct was 31 < NF-1
which is just a more complex way of saying NF>32
. 您最初的构造是
31 < NF-1
,这只是NF>32
的更复杂的表达方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.