在作为awk参数传递的多行上匹配多个regex

  • 本文关键字:regex awk 参数传递 bash awk
  • 更新时间 :
  • 英文 :


我正在尝试遍历一个大目录,并对每个文件运行不同的regexs,以获取以下数据;

  1. 文件名
  2. 图案匹配
  3. 上匹配的行
  4. 出现次数

多亏了@anubhava,我能够获得一个脚本,在多行中搜索一个正则表达式,并返回我需要的数据。

从那以后,我尝试调整(并删除(脚本,以匹配文件中的多个正则表达式,并返回所有正则表达式的数据。我可能在一个文件中查找多达8个regex模式。我现在正试图让它与脚本中硬编码的正则表达式一起工作,但最终我想将正则表达式模式作为args传递给脚本,并针对每个模式运行match命令。

这是目前的awk脚本,但它抛出了以下错误;

fatal: match: third argument is not an array+

脚本;

#!/usr/bin/awk -f
BEGIN {print ARGV[1], "(Filename)"}
{
RS = "r?n" 
filemsg= "new File() Found on line "
fismmsg= "FileInputStream Found on line "
while(match($0, /new[[:blank:]]+File(/, /FileInputStream/)) {
nf = match($0, /new[[:blank:]]+File(/)
fis = match($0, /FileInputStream/)
if (nf != ""){
print filemsg NR
++n
}
else if (fis != "") {
print fismmsg NR
++m
}
$0 = substr($0, RSTART+RLENGTH)
}
}
/new[[:blank:]]*$/ {
p = NR
next
}
/FileInputStream/ {
l = NR
next
}
p && NF {
if (/^[[:blank:]]*File(/) {
print filemsg p, "&", NR
++n
}
p = 0
}
l && NF {
if (/FileInputStream/) {
print fismmsg p, "&", NR
++m
}
}
END {
if (n > 0) {
print n, "(number of occurrences of new File() pattern)n"
}
else if (m > 0) {
print m, "(number of occurrences of FileInputStream pattern)n"
}
else {
print "No occurrences of new File() or FileInputStreamn"
}
}

我毫不怀疑我在做一些非常愚蠢的事情。

理想情况下,我会将每个正则表达式作为var传入,并在ARGV上迭代,以便在当前硬编码值所在的行中使用,但这也提出了一个问题,即当我们添加^[:blank:]]之类的元素来检查模式其余部分之前的行中是否有空格时,如何拆分该arg以使其能够在多行中使用。

更新

样本输入为:;

awk -v regex1="new[[:blank:]]+File(" -v regex2="FileInputStream" -v regex3="org\.apache\.commons\.net\.ftp\."-f parameterisedRegexAWKScript.awk "$file" >> "output.txt"'

样本输出为:;

./modules/configuration/config/rules/somerule.gr (Filename)
No occurrences of new File() 
./modules/configuration/upgrade/contact/somecontact.gs (Filename)
No occurrences of new File() 
./modules/configuration/entity/someentity.gsx (Filename)
No occurrences of new File() 
./modules/configuration/FTP/newFileTest.txt (Filename)
new File() Found on line 15
new File() Found on line 18
new File() Found on line 28
new File() Found on line 37
new File() Found on line 53
5 (number of occurrences of new File() pattern)
./modules/configuration/FTP/test.txt (Filename)
new File() Found on line 3
new File() Found on line 4 & 8
new File() Found on line 10
new File() Found on line 10
4 (number of occurrences of new File() pattern)
./modules/configuration/personaldata/someperson.gs (Filename)
No occurrences of new File() 
./modules/configuration/processes/someprocess.gs (Filename)
No occurrences of new File() 
./originalAwkScript.txt (Filename)
new File() Found on line 6
new File() Found on line 29
new File() Found on line 32
3 (number of occurrences of new File() pattern)

更新2

测试内容.tx

new
File()
new File()
new

File()
File() new
new File() test new File(Test)
FileInputStream

同一文件夹中另一个示例文件的内容;

protected function buildDocumentsPath(documentRootDir : String, documentTmpDir : String) {
if (DocumentsPathParameter.HasContent) {
DemoDocumentsPath = getAbsolutePath(DocumentsPathParameter, documentRootDir)
if (!new test 
File(DemoDocumentsPath).equals(new File(DocumentsPathParameter))) {
Logger.DOCUMENT.warn((typeof this).RelativeName)
DocumentsPath = getAbsolutePath(DocumentsPathParameter, documentTmpDir)
var file = new File(DocumentsPath)
if (!file.exists() && file.isDirectory()) {
file.mkdirs()
}
} 
}
}

但是输入文件可以是任何java类,它们没有什么特别之处。

需求汇总;从本质上讲,我试图使用bash命令解析一个大目录,该命令使用awk脚本搜索不同的regexs。这些正则表达式可以出现在类中的多行中,我需要捕获问题顶部列出的所有数据。我有不同的搜索类别,所以例如在FTP中,我要查找出现的"new File(","FileInputStream","org.apache.commons.net.FTP",java.nio.File",所以每个都有一个正则表达式,但还有其他类别,如打印(有不同的正则表达式(等。因此,理想情况下,我希望能够将我正在搜索的任何正则表达式作为params传递到awk脚本中,并将检索到的数据存储在文件中。

错误消息match: third argument is not an array表示您正在使用三个参数调用match()函数,而第三个参数不是预期的数组。

这是对match()的唯一一个具有三个参数的调用:

match($0, /new[[:blank:]]+File(/, /FileInputStream/)

根据接下来的几行判断,您希望匹配这两个正则表达式中的任何一个。那么你的线路应该是:

match($0, /new[[:blank:]]+File(|FileInputStream/)

最新更新