如何使用awk在CSV文件的一列中搜索完整的文本匹配,并且只输出第一个匹配行? [英] How to use awk to search for a complete match of text within one column of a CSV file and to only output the first matching line?
本文介绍了如何使用awk在CSV文件的一列中搜索完整的文本匹配,并且只输出第一个匹配行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的答案来自如何限制grep只搜索CSV文件中的一列,而是输出匹配的行?使用awk搜索特定的列,然后输出匹配的行,例如:
I am using the answer from How to restrict grep to search only the one column within a CSV file, but to output matching lines in their entirety? to search particular columns with awk, then output matching rows, e.g.:
awk -F@ "{if (\$2 ~ /$find_me/ ) { print \$0; } }" <input_file>
- 如何限制搜索仅匹配列匹配的行
$ find_me
完全? - 如何限制搜索只输出文件中找到的第一个匹配项?
- How can I restrict the search to only match lines where the column matches
$find_me
entirely? - How can I restrict the search to only output the first match found in the file?
推荐答案
您可以尝试:
awk -F@ -v pattern="$find_me" '$2 ~ "^" pattern "$"' input.txt
或
awk -F@ -v pattern="^$find_me\$" '$2 ~ pattern' input.txt
这篇关于如何使用awk在CSV文件的一列中搜索完整的文本匹配,并且只输出第一个匹配行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文