[英]How to use AWK to grab a row in a file by a certain column
1|1001|399.00|123
1|1001|29.99|234
2|1002|98.00|345
2|1002|29.98|456
3|1003|399.00|567
4|1004|234.56|456
How would I use awk to grab all the rows with '1002' in column 2? 我如何使用awk在第2列中使用'1002'获取所有行?
If I wanted to grab all the rows with '2' in the first column, I could use grep ^2, but how do I search by different columns? 如果我想在第一列中使用'2'获取所有行,我可以使用grep ^ 2,但如何使用不同的列进行搜索?
The typical solution is: 典型的解决方案是:
awk '$2 == 1002' FS=\| input-file
you get a slightly different result with: $2 ~ 1002
, which technically satisfies your query, but is probably not what you want. 你得到的结果略有不同: $2 ~ 1002
,技术上满足你的查询,但可能不是你想要的。 (It does a regex match, and so will match if the second column is "341002994"). (它执行正则表达式匹配,因此如果第二列是“341002994”则匹配)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.