bash 使用 AWK 过滤出具有数值范围的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8734351/
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 to filter out column with numerical ranges
提问by BurN135
I'm relatively new to BASH and I'm trying to use awk to filter out column 1 data based on the 4th column of a text file. If the 4th column of data matches the range of x, then it'll output column 1 data. "x" is suppose to be a range of numbers 1-10 (1,2,3..10).
我对 BASH 比较陌生,我正在尝试使用 awk 根据文本文件的第 4 列过滤掉第 1 列的数据。如果第 4 列数据与 x 的范围匹配,则它将输出第 1 列数据。“x”假设是数字 1-10 (1,2,3..10) 的范围。
awk -F: '{ if(=="x") print }' filename.txt
filename.txt
sample1 0 0 4
sample2 0 0 10
sample3 0 0 15
sample4 0 0 20
Actual use:
实际使用:
awk -F: '{ if(=="1-10") print }' sample.txt
output = sample1, sample2, sample3, sample4
It should be: sample1 sample2
only.
应该是:sample1 sample2
只有。
Is there is an error in the syntax that I'm not seeing or I could be possibly using this syntax completely wrong?
是否存在我没有看到的语法错误,或者我可能完全错误地使用了这种语法?
回答by Kambus
awk '{ if ( >= 1 && <= 10) print }' sample.txt
回答by olibre
awk ' ~ /^[1-9]$|^10$/{print }' sample.txt
output:
输出:
sample1
sample2
explanation:
解释:
^[1-9]$
--> $4 must be a single digit from 1 to 9|
(the pipe) --> or^10$
--> $4 must be the number 10
^[1-9]$
--> $4 必须是 1 到 9 之间的一位数|
(管道)--> 或^10$
--> $4 必须是数字 10
回答by Gregory Patmore
awk -F ':' ' >= 1 && <= 10{print }'
回答by gpojd
There may be a way to do it using only awk (nevermind, see my edit below), but I don't know of it. I'd combine it with grep:
可能有一种方法可以仅使用 awk(没关系,请参阅下面的编辑),但我不知道。我会将它与 grep 结合起来:
egrep ' ([1-9]|10)$' sample.txt | awk '{print }'
I think you are matching the fourth column with the string "1-10" not the range. Also, -F:
will change the delimiter to a colon rather than a space.
我认为您将第四列与字符串“1-10”而不是范围匹配。此外,-F:
将分隔符更改为冒号而不是空格。
Edit:
编辑:
awk ' ~ /^([1-9]|10)$/ {print }' sample.txt
回答by jaypal singh
If you want awk
to look up values from a range then you can set that range in the BEGIN
statement.
如果awk
要从某个范围中查找值,则可以在BEGIN
语句中设置该范围。
awk 'BEGIN{for (i=1;i<=10;i++) a[i]} ( in a){print }' sample.txt
Test:
测试:
[jaypal:~/Temp] cat sample.txt
sample1 0 0 4
sample2 0 0 10
sample3 0 0 15
sample4 0 0 20
[jaypal:~/Temp] awk 'BEGIN{for (i=1;i<=10;i++) a[i]} ( in a){print }' sample.txt
sample1
sample2
回答by Chris Koknat
If Perl is an option, you can try this solution similar to Kambus's awk solution:
如果 Perl 是一个选项,你可以尝试这个类似于 Kambus 的 awk 解决方案的解决方案:
perl -lane 'print $F[0] if $F[3] >= 1 && $F[3] <= 10' sample.txt
These command-line options are used:
使用这些命令行选项:
-n
loop around every line of the input file, do not automatically print every line-l
removes newlines before processing, and adds them back in afterwards-a
autosplit mode – split input lines into the @F array.-e
execute the perl code
-n
循环输入文件的每一行,不要自动打印每一行-l
在处理之前删除换行符,然后将它们添加回-a
自动拆分模式 – 将输入行拆分为 @F 数组。-e
执行perl代码
@F
is the array of words in each line, indexed starting with 0
@F
是每行中的单词数组,索引从 0 开始