在 awk 中打印第一个变量,但仅当它小于 X 时
我有一个包含单词的文件,我只需要打印小于或等于 4 个字符的行,但我的代码遇到了问题。行尾还有其他文本,但我在这里将其缩短。
file:
John Doe
Jane Doe
Mark Smith
Abigail Smith
Bill Adams
我想要做的是打印少于 4 个字符的名称。
我尝试过的:
awk '$1 <= 4 {print $1}' inputfile
我希望得到的:
John
Jane
Mark
Bill
到目前为止,我一无所获。它要么打印出所有内容,没有长度限制,要么根本不打印任何内容。有人可以看看这个并看看他们的想法吗? 谢谢
I have a file with words and I need to print only the lines that are less than or equal to 4 characters but I'm having trouble with my code. There is other text on the end of the lines but I shortened it for here.
file:
John Doe
Jane Doe
Mark Smith
Abigail Smith
Bill Adams
What I want to do is print the names that have less than 4 characters.
What I've tried:
awk '$1 <= 4 {print $1}' inputfile
What I'm hoping to get:
John
Jane
Mark
Bill
So far, I've got nothing. Either it prints out everything, with no length restrictions or it doesn't even print anything at all. Could someone take a look at this and see what they think?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,让我们理解为什么
给你整个
inputfile
,$1 <= 4
是数字比较,所以这会提示 GNUAWK
首先尝试转换列值到数值,但是 say 的数值是什么?作为 GNU
AWK
手动字符串Numbers 指出因此,从 GNU
AWK
的角度来看,John
的数值为零。为了获得所需的输出,您可以使用
length
函数,该函数返回字符数,如下所示,或者使用 0 到 4 个字符的模式匹配,其中
$1~
表示检查第一个字段匹配,.
表示任意字符,{0,4}
重复 0 到 4 次,^
字符串开头,$< /code> 字符串结尾(这两个是必需的,否则它也会匹配更长的字符串,因为它们确实包含子字符串
。{0,4}
)输入文件的两个代码都
给出输出
(在gawk 4.2.1中测试)
First, let understand why
gives you whole
inputfile
,$1 <= 4
is numeric comparison, so this prompt GNUAWK
to try to convert first column value to numeric value, but what is numeric value of say? As GNU
AWK
manual Strings And Numbers put itTherefore numeric value for
John
from GNUAWK
point of view is zero.In order to get desired output you might use
length
function which returns number of characters as followsor alternatively pattern matching from 0 to 4 characters that is
where
$1~
means check if 1st field match,.
denotes any character,{0,4}
from 0 to 4 repetitions,^
begin of string,$
end of string (these 2 are required as otherwise it would also match longer string, as they do contain substring.{0,4}
)Both codes for inputfile
give output
(tested in gawk 4.2.1)