string 使用awk在字符串中查找字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8481143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:18:43  来源:igfitidea点击:

find a string in a string using awk

stringawk

提问by AWE

here is column 6 in a file:

这是文件中的第 6 列:

ttttttttttt
tttttttttt
ttttttttt
tttttttattt
tttttttttt
ttttttttttt

how can I use awk to print out lines that include "a"

如何使用 awk 打印出包含“a”的行

采纳答案by Kevin

If you only want to search the sixth column, use:

如果只想搜索第六列,请使用:

awk ' ~ /a/' file

If you want the whole line, any of these should work:

如果你想要整条线,这些都应该有效:

awk /a/ file

grep a file

sed '/^[^a]*$/d' file

回答by jaypal singh

If you wish to print only those lines in which 6th column contains athen this would work -

如果您只想打印第 6 列包含的那些行,a那么这将起作用-

awk '$6~/a/' file

awk '$6~/a/' file

回答by Henry Barber

if it is an exact match (which yours is not) you're looking for:

如果它是一个完全匹配(你的不是)你正在寻找:

$6 == "a"

$6 == "a"

http://www.pement.org/awk/awk1line.txtis an excellent resource

http://www.pement.org/awk/awk1line.txt是一个很好的资源

awk can also tell you where the pattern is in the column:

awk 还可以告诉您模式在列中的位置:

awk '{++line_num}{ if ( match(,"a")) { print "found a at position",RSTART, " line " ,line_num}  }' file

though this example will only show the first "a" in column 6; a for loop would be needed to show all instances (I think)

虽然这个例子只会显示第 6 列中的第一个“a”;需要一个 for 循环来显示所有实例(我认为)

回答by souser

You could try

你可以试试

gawk '{ if (  ~ /a/ ) { print   } }' filename