简体   繁体   English

字段编号大于AWK的打印行

[英]Printing lines which have a field number greater than, in AWK

I am writing a script in bash which takes a parameter and storing it; 我在bash中编写一个脚本,它接受一个参数并存储它;

threshold = $1

I then have sample data that looks something like: 然后我有样本数据,看起来像:

5 blargh
6 tree
2 dog
1 fox
9 fridge

I wish to print only the lines which have their number greater than the number which is entered as the parameter (threshold). 我希望只打印数字大于作为参数(阈值)输入的数字的行。

I am currently using: 我目前正在使用:

awk '{print $1 > $threshold}' ./file

But nothing prints out, help would be appreciated. 但没有打印出来,帮助将不胜感激。

You're close, but it needs to be more like this: 你很接近,但它需要更像这样:

$ threshold=3
$ awk -v threshold="$threshold" '$1 > threshold' file

Creating a variable with -v avoids the ugliness of trying to expand shell variables within an awk script. 使用-v创建变量避免了在awk脚本中尝试扩展shell变量的awk

EDIT: 编辑:

There are a few problems with the current code you've shown. 您显示的当前代码存在一些问题。 The first is that your awk script is single quoted (good), which stops $threshold from expanding, and so the value is never inserted in your script. 第一个是你的awk脚本是单引号(好),它会阻止扩展的$threshold ,因此这个值永远不会插入你的脚本中。 Second, your condition belongs outside the curly braces, which would make it: 其次,你的病情属于大括号之外,这会使它成为:

$1 > threshold { print }

This works, but the `print is not necessary (it's the default action), which is why I shortened it to 这是有效的,但是`print不是必需的(这是默认动作),这就是为什么我将它缩短为

$1 > threshold

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM