sed - 如果找到,如何根据模式获取特定行

标签 sed awk

我试图从一个非常大的文件中收集文件名,具体取决于特定用户,在这种情况下 windowsdom\nasarchive被发现。

我试过运行 sed -nr "/-{3,}/h; /Path\s*:/H; /windowsdom\\nasarchive\s+Allow\s+FullControl/{x;G;p}" logfilename但它不会带来任何东西。

-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - April 21 - 2.doc


AccessToString : windowsdom\nasarchive Allow  FullControl
                 BUILTIN\Administrators Allow  FullControl
                 NT AUTHORITY\SYSTEM Allow  FullControl
                 BUILTIN\Users Allow  ReadAndExecute, Synchronize

-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2009\Credit status - Sept. 23 - 59.doc


AccessToString : windowsdom\acl_1 Allow  ReadAndExecute, Synchronize
                 windowsdom\acl_2 Allow  Modify, Synchronize
                 windowsdom\acl_3 Allow  ReadAndExecute, Synchronize
                 windowsdom\adm_server Allow  Modify, Synchronize
                 BUILTIN\Administrators Allow  FullControl


-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - August 10 - 3.doc


AccessToString : windowsdom\nasarchive Allow  FullControl
                 BUILTIN\Administrators Allow  FullControl
                 NT AUTHORITY\SYSTEM Allow  FullControl
                 BUILTIN\Users Allow  ReadAndExecute, Synchronize



-----------------------

预期结果:
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - April 21 - 2.doc
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - August 10 - 3.doc 

有人能想出如何得到预期的结果吗?

最佳答案

这可能对你有用(GNU sed):

sed -rn '/^Path:/h;/windowsdom\\nasarchive\s+Allow\s+FullControl/{g;p}' file

这将打印最后一个 Path string 当遇到所需的字符串时。

关于sed - 如果找到,如何根据模式获取特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588593/

相关文章:

php - 在 Markdown 文件中替换斜杠/破折号

image - 用于删除小图像的 Bash 脚本

linux - Excel 和 awk 不同意 CSV 总数

regex - Bash:第一次出现的字符之间的正则表达式匹配

shell - 比较文本文件中的日期| shell

linux - 在 Linux 中使用字典查找和替换单词

linux - 解析/proc/pid/smaps输出

linux - 行范围内的替换

linux - AWK 脚本 : Finding number of matches that each element in Col2 has in Col1

linux - 提取括号内的单词并将其替换为句子