使用AWK筛选出具有数字范围的列 [英] Using AWK to filter out column with numerical ranges

查看:971
本文介绍了使用AWK筛选出具有数字范围的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对BASH比较陌生,我正尝试使用awk根据文本文件的第4列过滤掉第1列数据.如果数据的第4列与x的范围匹配,则将输出第1列的数据.假设"x"是数字1-10(1,2,3..10)的范围.

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).

awk -F: '{ if($4=="x") print $1}' filename.txt

filename.txt 
sample1 0 0 4
sample2 0 0 10
sample3 0 0 15
sample4 0 0 20

实际使用:

awk -F: '{ if($4=="1-10") print $1}' sample.txt
output = sample1, sample2, sample3, sample4

应为:仅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?

推荐答案

awk '{ if ($4 >= 1 && $4 <= 10) print $1 }' sample.txt

这篇关于使用AWK筛选出具有数字范围的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆