使用 awk 检查字段长度

发布于 2024-10-11 09:51:06 字数 428 浏览 2 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-10-18 09:51:06

你所有的括号都不匹配。 'if' 表达式必须包含在括号内,即,

if (X == 45) ...
if ((X == 45) || (Y == 23)) ...

您没有这个,并且右括号比左括号多 - 所以平衡也被取消了;如果我们计算括号(打开时递增,关闭时递减),我们最终得到的总数是 -3 而不是 0,因此关闭的括号比打开的多三个:

            1      2  1       0          1  0     -1          0 -1                0 -1 -2     -3
awk -F, '{if(length($2) == 15 ) || length($2) == 16) && length($1) == 12 && length($3)  ) == 15) print  }'

所以,尝试这个来重新平衡所有内容:

awk -F, '{ if (((length($2) == 15 ) || length($2) == 16) && (length($1) == 12 && length($3) == 15)) print }'

All your brackets are mismatched. An 'if' expression must be contained within brackets, i.e.,

if (X == 45) ...
if ((X == 45) || (Y == 23)) ...

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:

            1      2  1       0          1  0     -1          0 -1                0 -1 -2     -3
awk -F, '{if(length($2) == 15 ) || length($2) == 16) && length($1) == 12 && length($3)  ) == 15) print  }'

So, try this instead which rebalances everything:

awk -F, '{ if (((length($2) == 15 ) || length($2) == 16) && (length($1) == 12 && length($3) == 15)) print }'
简单爱 2024-10-18 09:51:06

如果您要做的只是打印,则将表达式作为默认条件:

awk -F, 'length($1)==12 && (length($2)==15 || length($2)==16) && length($3)==15'

如果您尝试过滤掉输入文件中不符合此条件的行:

awk -F, '
    length($1)!=12 || length($2)<15 || length($2)>16 || length($3)!=15) {next}
    # do other stuff...
'

If all you're going to do is print, then put your expression as the default condition:

awk -F, 'length($1)==12 && (length($2)==15 || length($2)==16) && length($3)==15'

If you're trying to filter out lines in your input file that don't meet this criteria:

awk -F, '
    length($1)!=12 || length($2)<15 || length($2)>16 || length($3)!=15) {next}
    # do other stuff...
'
一抹苦笑 2024-10-18 09:51:06

您的条件括号不匹配/不平衡。尝试:

{ if ((length($2) == 15 || length($2) == 16) && length($1) == 12 && length($3) == 15) print; }

You have mismatched/unbalanced parenthesis in conditional. Try:

{ if ((length($2) == 15 || length($2) == 16) && length($1) == 12 && length($3) == 15) print; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文