【发布时间】: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