使用 awk 检查字段长度
我有一个包含 3 个字段的文件:
123710337783,351898014413150,123028040249634
123710337785,352934028758390,123028040109275
我需要检查这些字段是否满足以下长度:
Field 1 = 12
Field 2 = 15 or 16
Field 3 = 15
运行此命令时出现错误:
awk -F, '{if(length($2) == 15 ) || length($2) == 16) && length($1) == 12 && length($3)) == 15) print }'
请协助。
伯尼
I have a file of 3 fields:
123710337783,351898014413150,123028040249634
123710337785,352934028758390,123028040109275
I need to check that the fields meet the following lengths:
Field 1 = 12
Field 2 = 15 or 16
Field 3 = 15
I get an error when running this:
awk -F, '{if(length($2) == 15 ) || length($2) == 16) && length($1) == 12 && length($3)) == 15) print }'
Please assist.
Bernie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所有的括号都不匹配。 'if' 表达式必须包含在括号内,即,
您没有这个,并且右括号比左括号多 - 所以平衡也被取消了;如果我们计算括号(打开时递增,关闭时递减),我们最终得到的总数是 -3 而不是 0,因此关闭的括号比打开的多三个:
所以,尝试这个来重新平衡所有内容:
All your brackets are mismatched. An 'if' expression must be contained within brackets, i.e.,
you don't have this, and you've got more closing brackets than opening brackets - so the balance is off as well; if we count the brackets (increment for open, decrement for close), we end up with a total of -3 instead of 0, so closing three more brackets than we have open:
So, try this instead which rebalances everything:
如果您要做的只是打印,则将表达式作为默认条件:
如果您尝试过滤掉输入文件中不符合此条件的行:
If all you're going to do is print, then put your expression as the default condition:
If you're trying to filter out lines in your input file that don't meet this criteria:
您的条件括号不匹配/不平衡。尝试:
You have mismatched/unbalanced parenthesis in conditional. Try: