Linux 使用带有列值条件的 awk
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14739057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using awk with column value conditions
提问by user1687130
I'm learning awk from The AWK Programming Languageand I have a problem with one of the examples.
我正在从The AWK Programming Language学习 awk并且我对其中一个示例有问题。
If I wanted to print $3 if $2 is equal to a value (e.g.1
), I was using this command which works fine:
如果我想在 $2 等于一个值(例如1
)时打印 $3 ,我正在使用这个工作正常的命令:
awk '==1 {print }' <infile> | more
But when I substitute 1 by another searching criteria, (e.g.findtext
), the command doesn't work:
但是当我用另一个搜索条件(例如findtext
)替换 1 时,该命令不起作用:
awk '== findtext {print }' <infile> | more
It returns no output and I'm sure that 'findtext' exist on the input file.
它不返回任何输出,我确定输入文件中存在“findtext”。
I also tried this, but it does not work:
我也试过这个,但它不起作用:
awk '== "findtext" {print }' <infile> | more
Here's my test file named 'test' and it has 9 lines and 8 fields, separated by space:
这是我的名为“test”的测试文件,它有 9 行和 8 个字段,用空格分隔:
1 11 0.959660297 0 0.021231423 -0.0073 -0.0031 MhZisp
2 14 0.180467091 0.800424628 0 0.0566 0.0103 ClNonZ
3 19 0.98089172 0 0 -0.0158 0.0124 MhNonZ
4 15 0.704883227 0.265392781 0.010615711 -0.0087 -0.0092 MhZisp
5 22 0.010615711 0.959660297 0.010615711 0.0476 0.0061 ClNonZ
6 23 0.715498938 0 0.265392781 -0.0013 -0.0309 Unkn
7 26 0.927813163 0 0.053078556 -0.0051 -0.0636 MhZisp
8 44 0.55626327 0.222929936 0.201698514 0.0053 -0.0438 MhZisp
9 31 0.492569002 0.350318471 0.138004246 0.0485 0.0088 ClNonZ
Here's what I did and the output:
这是我所做的和输出:
$awk ' == "ClNonZ" {print }' test
$ grep ClNonZ test
2 14 0.180467091 0.800424628 0 0.0566 0.0103 ClNonZ
5 22 0.010615711 0.959660297 0.010615711 0.0476 0.0061 ClNonZ
9 31 0.492569002 0.350318471 0.138004246 0.0485 0.0088 ClNonZ
I expect to see this which is the $3 that has "ClNonZ" in their $8.
我希望看到这是 3 美元,在他们的 8 美元中有“ClNonZ”。
0.180467091
0.010615711
0.492569002
Don't know why the awk command didn't return anything. Any thoughts?
不知道为什么 awk 命令没有返回任何内容。有什么想法吗?
采纳答案by Rob Davis
If you're looking for a particular string, put quotes around it:
如果您要查找特定字符串,请在其周围加上引号:
awk ' == "findtext" {print }'
Otherwise, awk will assume it's a variable name.
否则,awk 将假定它是一个变量名。
回答by arutaku
Depending on the AWK
implementation are you using ==
is ok or not.
取决于AWK
您使用的实现是否可以==
。
Have you tried ~
?. For example, if you want $1 to be "hello":
你试过~
吗?。例如,如果您希望 $1 为“hello”:
awk ' ~ /^hello$/{ print ; }' <infile>
^
means $1 start, and $
is $1 end.
^
表示 $1 开始,$
$1 结束。
回答by user1687130
My awk version is 3.1.5.
我的 awk 版本是 3.1.5。
Yes, the input file is space separated, no tabs.
是的,输入文件是空格分隔的,没有制表符。
According to arutaku's answer, here's what I tried that worked:
根据 rutaku 的回答,这是我尝试过的有效方法:
awk ' ~ "ClNonZ"{ print ; }' test
0.180467091
0.010615711
0.492569002
$ awk ' ~ "ClNonZ" { print }' test
0.180467091
0.010615711
0.492569002
What didn't work(I don't know why and maybe due to my awk version:),
什么不起作用(我不知道为什么,也许是因为我的 awk 版本:),
$awk ' ~ "^ClNonZ$"{ print ; }' test
$awk ' == "ClNonZ" { print }' test
Thank you all for your answers, comments and help!
感谢大家的回答、评论和帮助!
回答by Mustafa
please try this
请试试这个
echo $VAR | grep ClNonZ | awk '{print }';
or
或者
echo cat filename | grep ClNonZ | awk '{print }';
回答by Ell
This method uses regexp, it should work:
此方法使用正则表达式,它应该可以工作:
awk ' ~ /findtext/ {print }' <infile>
回答by user2773013
This is more readable for me
这对我来说更具可读性
awk '{if ( ~ /findtext/) print }' <infile>