0

If anybody can help me to find out and answer for following case, it would be much appreciated. I need a solution which should be able to execute in bash shell.

Suppose I have file like this,

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.

My search criteria is like this,

It should first search for word little (i.e. line 1 2 and 3) and in the next row there should be a word fleece (i.e. line 4 only) the output should print two matching lines (3 and 4) plus four lines after last searched line which is by fleece at line number 4. So final output should be lines 3,4,5,6,7,8

Solution could be a bash shell script, perl or python.

Please share your ideas.

serenesat
  • 4,611
  • 10
  • 37
  • 53
Doonyx
  • 580
  • 3
  • 12
  • could you explain this ` the output should print two matching lines (3 and 4)` ? – Avinash Raj Feb 16 '15 at 04:46
  • @Avinash Raj This 'grep' should first search word 'little' then in the immediate line there should be a word 'fleece'. If there are such line combinations, then such combination should be printed. Thus line 3 and 4. Moreover all the lines from 5 to 8 should also be printed. – Doonyx Feb 16 '15 at 04:49
  • but there isn't strings like `fleece` and `little` on the same line. – Avinash Raj Feb 16 '15 at 04:51
  • @Avinash Raj Yes. That is the problem. I'd say it again. First it should search for word 'little' once such line found, then in immediate line there should be a word 'fleece'. But not in the same line. This has been explained in http://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep but my problem is how to get N number lines after last mach. – Doonyx Feb 16 '15 at 04:53
  • did you wanna grep solution? – Avinash Raj Feb 16 '15 at 04:55
  • @Avinash Raj Whatever solution, it could run in bash shell (we can assume we have perl and python) – Doonyx Feb 16 '15 at 04:56
  • For the person who down voted to my question. If you feel "this question does not show any research effort. it is unclear or not useful". Please elaborate. – Doonyx Feb 16 '15 at 05:00
  • I'm not the downvoter, but showing what you attempted and how it failed would usually be good form. – tripleee Feb 16 '15 at 07:36

2 Answers2

1

Through 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

REGEX DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

Somewhat less befuddlingly, here is a simple Awk script.

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
tripleee
  • 175,061
  • 34
  • 275
  • 318