问题描述:

I have the following 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: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)/?$";

What I want to do is to search for the word jobman and delete the whole entry that word was found. The delimiters for each entry is the semicolon ";". I need to do that from Mac OS command line. So I have tools as grep,fgrep and awk available.

网友答案:

Firstly, what we need to delete from that text?

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

If it's correct, then

$> 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]";

What we actually doing in "s/[^;]*jobman[^;]*;//g" is searching for [^;]*jobman[^;]*; group of symbols (not : any times, jobman, not : any times and ;). Than we substitute it with ''. And doing that substitution for all text lines.

相关阅读:
Top