Shell 如何grep精确匹配并仅打印该匹配
我有文件的大量数据,需要以以下格式打印准确的关键字:Shell 如何grep精确匹配并仅打印该匹配,shell,awk,grep,Shell,Awk,Grep,我有文件的大量数据,需要以以下格式打印准确的关键字:Filename:keyword:linenumber [注意:需要在整个目录中递归搜索关键字] 例如:我想搜索关键字:abcxyz 文件中的数据如下所示 abcxyz.fgh gfhj.abcxyz i have a book with name abcxyz.sh where is my brother called : abcxyz.fsdghj raju how are you afsgdjj kllf ghjakl ra
Filename:keyword:linenumber
[注意:需要在整个目录中递归搜索关键字]
例如:我想搜索关键字:abcxyz
文件中的数据如下所示
abcxyz.fgh
gfhj.abcxyz
i have a book with name abcxyz.sh
where is my brother called : abcxyz.fsdghj raju how are you
afsgdjj kllf
ghjakl ra jksu ldlahlalfb afhkflkaf dbllf jll afl;bnhafl
当我使用以下命令时:grep“abcxyz”*.*.
它正在打印我不需要的整行内容
预期输出:
Filename : abcxyz.fgh : line number
Filename : gfhj.abcxyz : line number
Filename : abcxyz.sh : line number
Filename : abcxyz.fsdghj : line number
这应该是
awk
的一项工作,请您尝试以下内容,使用GNUawk
中显示的示例编写和测试。请使用绝对路径代替
,以便在find命令中为任何目录运行它
对于所有文件,输出应为filename:matched string(s):行号
您可以运行以下find
命令:
find . -type f -exec awk -f script.awk {} +
其中script.awk
如下所示:
cat script.awk
BEGIN{ OFS=" : " }
NF{
val=""
for(i=1;i<=NF;i++){
if($i~/abcxyz/){
val=(val?val OFS:"")$i
}
}
if(val){
print FILENAME,val,FNR
}
}
说明:添加上述内容的详细说明
BEGIN{ OFS=" : " } ##Setting OFS to space colon space in BEGIN section of this program.
NF{ ##Checking condition if line is NOT empty then do following.
val=""
for(i=1;i<=NF;i++){ ##Traversing through all field values here.
if($i~/abcxyz/){ ##checking condition if field is matching abcxyz then do following.
val=(val?val OFS:"")$i ##Creating val which has value of current field and keep adding it.
}
}
if(val){ ##Checking condition if val is NOT NULL then do following.
print FILENAME,val,FNR ##Printing FILENAME val and FNR here.
}
}
'
BEGIN{OFS=“:”}##在该程序的BEGIN部分将OFS设置为空格冒号空格。
NF{{###检查条件,若行不为空,则执行以下操作。
val=“”
对于(i=1;i这应该是awk
的作业,请尝试在GNUawk
中使用显示的示例进行以下操作、编写和测试。请使用绝对路径代替
,以便为find命令中的任何目录运行它
对于所有文件,输出应为filename:matched string(s):行号
您可以运行以下find
命令:
find . -type f -exec awk -f script.awk {} +
其中script.awk
如下所示:
cat script.awk
BEGIN{ OFS=" : " }
NF{
val=""
for(i=1;i<=NF;i++){
if($i~/abcxyz/){
val=(val?val OFS:"")$i
}
}
if(val){
print FILENAME,val,FNR
}
}
说明:添加上述内容的详细说明
BEGIN{ OFS=" : " } ##Setting OFS to space colon space in BEGIN section of this program.
NF{ ##Checking condition if line is NOT empty then do following.
val=""
for(i=1;i<=NF;i++){ ##Traversing through all field values here.
if($i~/abcxyz/){ ##checking condition if field is matching abcxyz then do following.
val=(val?val OFS:"")$i ##Creating val which has value of current field and keep adding it.
}
}
if(val){ ##Checking condition if val is NOT NULL then do following.
print FILENAME,val,FNR ##Printing FILENAME val and FNR here.
}
}
'
BEGIN{OFS=“:”}##在该程序的BEGIN部分将OFS设置为空格冒号空格。
NF{{###检查条件,若行不为空,则执行以下操作。
val=“”
对于(i=1;i给你
grep-roHn“\S*您的\u文本\u此处\S*”*
标签
-r:递归地在目录中
-o:只有匹配的部分
-H:用文件名
-n:有行号吗
然后使用\S
调整正则表达式,使其包含匹配模式周围除空格、制表符和换行符以外的所有字符。
注意:如果您只需要字母和数字,而不需要特殊符号,请使用\w
最后一个“*”表示搜索当前目录中的每个文件和文件夹。因此,要先使用此命令cd
到所需的目录。开始
grep-roHn“\S*您的\u文本\u此处\S*”*
标签
-r:递归地在目录中
-o:只有匹配的部分
-H:用文件名
-n:有行号吗
然后使用\S
调整正则表达式,使其包含匹配模式周围除空格、制表符和换行符以外的所有字符。
注意:如果您只需要字母和数字,而不需要特殊符号,请使用\w
最后一个“*”表示搜索当前目录中的每个文件和文件夹。因此,首先使用此命令将cd
搜索到所需目录。有趣的是,您对问题的解释与我的相反。我可以理解。我们的答案之一可能不是OP想要的。我完全确定我的阅读。但你可能也是。让我看看。看起来你读对了。玩得开心。为什么你用*
作为路径,而不是
作为简单的“这里”呢?我习惯使用*
。我通常需要选择具有特定格式的文件,如“*.txt”。
完全适用于这种情况。您应该使用\S
而不是\w
,因为OP希望匹配的不仅仅是单词组成字符,例如
,请参阅预期输出。您还应该提及此要求ires GNU grep。有趣的是,你对这个问题的解释与我的相反。我能理解。我们的答案之一可能不是OP想要的。我完全肯定我的阅读。但你可能也是。让我看看。看起来你读得对。玩得开心。你为什么用*
作为路径,而不是
>对于简单的“这里”?我习惯使用*
。我通常需要选择具有特定格式的文件,如“*.txt”。
完全适用于这种情况。您应该使用\S
而不是\w
,因为OP希望匹配的不仅仅是单词组成字符,例如
,请参阅预期输出。您还应该提及此要求格雷普。