在 awk 中打印第一个变量,但仅当它小于 X 时

发布于 2025-01-19 11:41:13 字数 400 浏览 3 评论 0原文

我有一个包含单词的文件,我只需要打印小于或等于 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 技术交流群。

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

发布评论

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

评论(1

岁月静好 2025-01-26 11:41:13

首先,让我们理解为什么

awk '$1 <= 4 {print $1}' inputfile

给你整个 inputfile$1 <= 4 是数字比较,所以这会提示 GNU AWK 首先尝试转换列值到数值,但是 say 的数值是什么

John

?作为 GNU AWK 手动字符串Numbers 指出

通过解释任何数字前缀将字符串转换为数字
字符串作为数字(...)不能解释为的字符串
有效数字转换为零。

因此,从 GNU AWK 的角度来看,John 的数值为零。

为了获得所需的输出,您可以使用 length 函数,该函数返回字符数,如下所示,

awk 'length($1)<=4{print $1}' inputfile

或者使用 0 到 4 个字符的模式匹配,其中

awk '$1~/^.{0,4}$/{print $1}' inputfile

$1~ 表示检查第一个字段匹配,. 表示任意字符,{0,4} 重复 0 到 4 次,^ 字符串开头,$< /code> 字符串结尾(这两个是必需的,否则它也会匹配更长的字符串,因为它们确实包含子字符串。{0,4}

输入文件的两个代码都

John Doe 
Jane Doe
Mark Smith
Abigail Smith
Bill Adams

给出输出

John
Jane
Mark
Bill

(在gawk 4.2.1中测试)

First, let understand why

awk '$1 <= 4 {print $1}' inputfile

gives you whole inputfile, $1 <= 4 is numeric comparison, so this prompt GNU AWK to try to convert first column value to numeric value, but what is numeric value of say

John

? As GNU AWK manual Strings And Numbers put it

A string is converted to a number by interpreting any numeric prefix
of the string as numerals(...)Strings that can’t be interpreted as
valid numbers convert to zero.

Therefore numeric value for John from GNU AWK point of view is zero.

In order to get desired output you might use length function which returns number of characters as follows

awk 'length($1)<=4{print $1}' inputfile

or alternatively pattern matching from 0 to 4 characters that is

awk '$1~/^.{0,4}$/{print $1}' inputfile

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

John Doe 
Jane Doe
Mark Smith
Abigail Smith
Bill Adams

give output

John
Jane
Mark
Bill

(tested in gawk 4.2.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文