如果有人可以帮助我找到并回答以下案例,我们将不胜感激。我需要一个能够在bash shell
中执行的解决方案。
假设我有这样的文件,
1.Mary had a little lamb,
2.Little lamb, little lamb,
3.Mary had a little lamb,
4.Its fleece was white as snow
5.And everywhere that Mary went,
6.Mary went, Mary went,
7.Everywhere that Mary went
8.The lamb was sure to go
9.It followed her to school one day
10.School one day, school one day
11.It followed her to school one day
12.Which was against the rules.
我的搜索条件是这样的,
首先应该搜索单词little
(即第1行和第2行),在下一行中应该有一个单词fleece
(即仅限第4行)输出应该打印两个匹配的行(3和4)加上最后一行搜索行之后的四行,在第4行的fleece
之后。最终输出应为lines 3,4,5,6,7,8
解决方案可以是bash shell脚本,perl或python。
请分享您的想法。
答案 0 :(得分:1)
通过grep,
$ grep -oPz '(?s)[^\n]*\blittle\b(?:(?!\blittle\b|\bfleece\b).)*fleece[^\n]*(?:\n[^\n]*){4}' file
3.Mary had a little lamb,
4.Its fleece was white as snow
5.And everywhere that Mary went,
6.Mary went, Mary went,
7.Everywhere that Mary went
8.The lamb was sure to go
答案 1 :(得分:1)
稍微不那么迷茫,这是一个简单的Awk脚本。
awk '/little/ { find_fleece=1; kept=$0; next }
find_fleece { find_fleece=0; if ($0 ~ /fleece/) {
print kept; print; tail=4; } else { tail=0 }; next }
tail-- > 0' file