tags:

views:

21

answers:

2

I want to find a pattern which is nearest to a specific pattern. Such as I want to print "bbb=" which is under the "yyyy:" (it is the closest line with bbb= to yyyy). It is line 8. line numbers and the order might be changed so it is better not to use line numbers.

root# vi a
"a" 15 lines

 1  ## xxxx:

 2  aaa=3

 3  bbb=4

 4  ccc=2

 5  ddd=1

 6  ## yyyy:

 7  aaa=1

 8  bbb=0

 9  ccc=3

10  ddd=3

11  ## zzzz:

12  aaa=1

13  bbb=1

14  ccc=1

15  ddd=1

Do you have an idea using awk or grep for this purpose?

+1  A: 
schot
A: 

Quick and dirty using grep:

grep -A 100 '##yyyy' filename | grep 'bbb='
Dennis Williamson