There is a server log page - this type:

<html> <head></head> <titile></title> <body> <table> <tr></tr> <tr></tr> <tr></tr> </table> #Много таких блоков <table> <tr></tr> <tr></tr> <tr></tr> </table> 

It is quite normal to do this kind of constellation.

 cat index.html | grep "Нужное событие:" -A 4 -B 21 

Which was looking for the right combination of words and showed 4 lines below - it was the beginning of the "table" block and, accordingly, 21 lines above, where the beginning of the table block was.

Everything was fine, but there were some changes, and the number of tr-tr lines began to change, then more, then less.

And accordingly, and so it would be good to do at the very beginning, put the search / output between the table-table blocks

But I don’t understand how to implement bash ... grep didn't give me hints, but with sed I can only replace the block between the lines, but I don’t see how I can point it out ...

Tell me. Thank.

  • And inside the <table> somewhere deeper than the other <table> I hope does not happen? And I correctly understood that we need a complete table block within which somewhere there is some key text? - Mike
  • Inside the table there are no other tables) And yes, the block between the table / table is needed completely - Sober

1 answer 1

 cat index.html | sed -n '/<table>/{h;bo};H;/<\/table>/{g;/Нужное событие:/p};:o' 

Decryption:

 /<table>/{ # Когда видим <table> h; # заменяем текущий "hold space" (очищаем предыдущий) bo}; # И уходим на метку "o" H; # добавляем текущую строку в буфер "hold space" /<\/table>/{ # Если таблица заканчивается (</table>) g; # Извлекаем все из "hold space" /событие:/p}; # Если в накопленном буфере есть нужный текст печатаем его :o # Метка для перехода "o", конец обработки строки 
  • and in sed'e you can remove the sensitivity of the register? in grep, this is -i, and sed? - Sober
  • Add I to the expression, i.e. for event search and print /событие:/Ip - Mike