【问题标题】:Recursive Search of Multiple Occurrences of Multiple Strings多个字符串多次出现的递归搜索
【发布时间】:2013-05-27 15:47:25
【问题描述】:

这个问题是之前question 的方向延伸。下面提供了我的搜索要求

需要搜索的多个字符串存储在文件 values.txt(输入文件)中,例如包含如下信息

string1  1
string2  3
string3  5
  • 其中第一列(string1、string2、string3)表示 需要搜索,而第二列表示的数量 要搜索的事件。
  • 进一步的搜索需要在文件上递归执行 特定的文件扩展名(例如 .out、.txt 等)
  • 搜索输出应定向到一个文件,搜索输出与文件名及其路径一起打印在该文件中。

例如,典型输出必须如下所示(用于递归搜索扩展名为 .out 的文件名)

<path_of_searched_file1/fileName1.out>
The full line containing the <first> instance of <string1>
The full line containing the <first> instance of <string2>
The full line containing the <second> instance of <string2>
The full line containing the <third> instance of <string2>
The full line containing the <first> instance of <string3>
The full line containing the <second> instance of <string3>
The full line containing the <third> instance of <string3>
The full line containing the <fourth> instance of <string3>
The full line containing the <fifth> instance of <string3>


<path_of_searched_file2/fileName2.out>
The full line containing the <first> instance of <string1>
The full line containing the <first> instance of <string2>
The full line containing the <second> instance of <string2>
The full line containing the <third> instance of <string2>
The full line containing the <first> instance of <string3>
The full line containing the <second> instance of <string3>
The full line containing the <third> instance of <string3>
The full line containing the <fourth> instance of <string3>
The full line containing the <fifth> instance of <string3>


and so on

使用 awk 是解决此搜索问题的最佳方法吗?如果是这样,有人可以帮我修改以前的question 中提供的 awk 代码,以满足我当前的搜索要求。

【问题讨论】:

  • 您能否详细说明要搜索的出现次数 的含义?是每个文件吗?每行?
  • 嗨,例如 value1 必须仅搜索文件中的第一次出现。 value2 必须搜索前 3 个出现,value3 搜索前 5 个出现。
  • 发布一些小样本输入和预期输出。
  • @Morton 我已经编辑了问题以提供我期望的示例 I/O
  • @Steve 非常感谢。我将在几个小时内根据我的需要实施此操作,然后接受您的回答。如果您可以在答案中指出一些关于 awk 编程的参考资料(最好是在线参考),这也会有所帮助。这将使我和可能的其他人能够更多地了解 awk 编程。

标签: search recursion awk grep gawk


【解决方案1】:

这是使用awk 的一种方式; YMMV。运行方式:

awk -f ./script.awk values.file $(find . -type f -regex ".*\.\(txt\|doc\|etc\)$")

script.awk的内容:

FNR==NR {
    a[$1]=$2;
    next
}

FNR==1 {
    for (i in a) {
        b[i]=a[i]
    }
}

{
    for (j in b) {
        if ($0 ~ j && b[j]-- > 0) {
            print > FILENAME ".out"
        }
    }
}

或者,这里是单行:

awk 'FNR==NR { a[$1]=$2; next } FNR==1 { for (i in a) b[i]=a[i] } { for (j in b) if ($0 ~ j && b[j]-- > 0) print > FILENAME ".out" }' values.file $(find . -type f -regex ".*\.\(txt\|doc\)$")

解释:

在第一个块中,创建一个关联数组,其中values.file 的第一列作为键,第二列作为值。第二个和第三个块读取使用find 命令找到的文件。在第一个块中形成的数组是重复的(使用awk 没有简单的方法来做到这一点;所以也许Perl 和Find::File::Rule 模块会是更好的选择?)对于找到的每个文件。在第三个块中,我们循环遍历每个键,搜索字符串并递减它的值,打印到带有“.out”扩展名的文件位置。

【讨论】:

  • 应要求,我的学习建议:我开始here并完成了教程。然后我开始在这个标签下阅读和回答关于 SO 的问题。我强烈建议任何想要学习awk 的人开始做同样的事情。在这个网站上,有大量的专家 'awker' 可以帮助你走上正轨。此外,为这个网站做出贡献的人越多,我要做的工作就越少。这是另一个reference。 HTH。
猜你喜欢
  • 2013-05-06
  • 1970-01-01
  • 2012-03-30
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多