2011-11-19 85 views
0

我有以下文字:如何搜索和刪除裏面分隔符的文本文件模式

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)/?$";

我想要做的是搜索單詞感謝達雷爾喬布曼和刪除整個條目字被發現。每個條目的分隔符都是分號「;」。我需要從Mac OS命令行執行此操作。所以我有工具作爲grep,fgrep和awk可用。

回答

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,沒有任何:時間和)。比我們用''代替它。並對所有文本行進行替換。

相關問題