【问题标题】:How to search and delete for pattern inside delimiters text file如何在分隔符文本文件中搜索和删除模式
【发布时间】:2011-11-19 05:15:09
【问题描述】:

我有以下文字:

s:50:"index.php?attachment=$matches[1]&cpage=$matches[2]";s:44:"(term-conditions-for-employers)/trackback/?$";s:35:"index.php?pagename=$matches[1]&tb=1";s:71:"(term-conditions-for-employers)/feed/(feed|rdf|rss|rss2|atom|jobman)/?$";s:47:"index.php?pagename=$matches[1]&feed=$matches[2]";s:66:"(term-conditions-for-employers)/(feed|rdf|rss|rss2|atom|jobman)/?$";s:47:"index.php?pagename=$matches[1]&feed=$matches[2]";s:52:"(term-conditions-for-employers)/page/?([0-9]{1,})/?$";s:48:"index.php?pagename=$matches[1]&paged=$matches[2]";s:59:"(term-conditions-for-employers)/comment-page-([0-9]{1,})/?$";s:48:"index.php?pagename=$matches[1]&cpage=$matches[2]";s:44:"(term-conditions-for-employers)(/[0-9]+)?/?$";s:47:"index.php?pagename=$matches[1]&page=$matches[2]";s:26:"home/attachment/([^/]+)/?$";s:32:"index.php?attachment=$matches[1]";s:36:"home/attachment/([^/]+)/trackback/?$";s:37:"index.php?attachment=$matches[1]&tb=1";s:63:"home/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom|jobman)/?$";s:49:"index.php?attachment=$matches[1]&feed=$matches[2]";s:58:"home/attachment/([^/]+)/(feed|rdf|rss|rss2|atom|jobman)/?$";

我想要做的是搜索单词 jobman 并删除找到该单词的整个条目。每个条目的分隔符是分号“;”。我需要从 Mac OS 命令行执行此操作。所以我有 grep、fgrep 和 awk 等工具可用。

【问题讨论】:

    标签: macos sed awk grep delimiter


    【解决方案1】:

    首先,我们需要从该文本中删除什么?

    $> grep -o -P "[^;]*jobman[^;]*;" ./text 
    s:71:"(term-conditions-for-employers)/feed/(feed|rdf|rss|rss2|atom|jobman)/?$";
    s:66:"(term-conditions-for-employers)/(feed|rdf|rss|rss2|atom|jobman)/?$";
    s:63:"home/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom|jobman)/?$";
    s:58:"home/attachment/([^/]+)/(feed|rdf|rss|rss2|atom|jobman)/?$";
    

    如果正确,那么

    $> sed "s/[^;]*jobman[^;]*;//g" ./text 
    s:50:"index.php?attachment=$matches[1]&cpage=$matches[2]";s:44:"(term-conditions-for-employers)/trackback/?$";s:35:"index.php?pagename=$matches[1]&tb=1";s:47:"index.php?pagename=$matches[1]&feed=$matches[2]";s:47:"index.php?pagename=$matches[1]&feed=$matches[2]";s:52:"(term-conditions-for-employers)/page/?([0-9]{1,})/?$";s:48:"index.php?pagename=$matches[1]&paged=$matches[2]";s:59:"(term-conditions-for-employers)/comment-page-([0-9]{1,})/?$";s:48:"index.php?pagename=$matches[1]&cpage=$matches[2]";s:44:"(term-conditions-for-employers)(/[0-9]+)?/?$";s:47:"index.php?pagename=$matches[1]&page=$matches[2]";s:26:"home/attachment/([^/]+)/?$";s:32:"index.php?attachment=$matches[1]";s:36:"home/attachment/([^/]+)/trackback/?$";s:37:"index.php?attachment=$matches[1]&tb=1";s:49:"index.php?attachment=$matches[1]&feed=$matches[2]";
    

    我们在"s/[^;]*jobman[^;]*;//g" 中实际做的是搜索[^;]*jobman[^;]*; 符号组(不是: 任何时候,jobman,不是: 任何时候和;)。比我们用''代替它。并对所有文本行进行替换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-15
      • 2019-05-15
      • 2016-05-03
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2015-02-06
      相关资源
      最近更新 更多