简体   繁体   English

如何使用AWK通过某列来获取文件中的行

[英]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.

相关问题 如何仅在特定行和特定列中使用 awk - How to awk only in a certain row and specific column shell / awk —如果另一文件中存在列,则打印行的某些字段 - shell/awk — print certain fields of a row if a column exists in another file 如何使用awk计算特定列中的特定位数 - How to use awk counting the number of specificed digit in certain column 如果它们与 file2 中第 1 列中的列表匹配,我如何使用 awk 仅打印 file1 给定行中的值? - How can I use awk to only print values in a given row of file1 if they match with a list in column 1 in file2? 如何使用`awk`来grep某些像这样的列? - How to use `awk` to grep certain columns like these? 如何使用 awk/sed 仅替换某些行中特定列中的数据 - How to use awk/sed to replace data in a specific column in certain rows only 如何使用 sed 或 awk 将 file1 的一部分替换为 file2 的一部分,部分由行和列描述 - How to replace a portion of file1 by a portion of file2 uing sed or awk, portion delineated by row and column no 如何使用 awk 从文件中选择文本,从行号开始直到某个字符串 - How to use awk to select text from a file starting from a line number until a certain string 如何查找文件中是否存在行并使用awk添加带有文件名的列? - How can I find if a row exists in a file and add a column with the filename using awk? 用awk在列中打印某些参数 - Print certain parameter in column with awk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM