Показывать только часть лог-файла в скрипте bash

201
Lennart Rolland

Журнал OpenVPN выглядит так:

OpenVPN  CLIENT LIST Updated,Sat Jun 20 04:20:07 2015 Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since [ A .... ] ROUTING TABLE Virtual Address,Common Name,Real Address,Last Ref [ B.... ] GLOBAL STATS Max bcast/mcast queue length,33 END 

Где A и B выше - это списки хостов, отформатированные определенным образом.

У меня есть сценарий bash, который я буду использовать, чтобы воздействовать на элементы в части, называемой «ROUTING TABLE» ( B ). Как мне эффективно отделить этот список хостов от остальной части документа в моем сценарии?

-1

2 ответа на вопрос

1
MariusMatutiae

The following sed command

 sed -n '/ROUTING/,/\[ B/' filename 

does what you wish: the -n suppresses the default behaviour which is to print every line, the expression between single quotes selects a range of lines, then prints them (this is the meaning of ). The ranges to be selected are identified by means of matches (the initial and final one) delimited by separatrices, in my case I use the forward slash / as separatrix; the initial and final pattern match must be separated by a comma, and \[ is used to stress the literal meaning of the square parenthesis, i.e. this is not a grammatical structure but instead it is a real opening square parenthesis which must be found.

1
meuh

You can used sed to delete (d) lines from line 1 to the matching start line. Then delete lines from the matching end line to the last line ($). You probably also want to delete the "Virtual Address" header line too. All that's left is what you want!

sed '1,/^ROUTING TABLE/d /^Virtual Address,/d /^GLOBAL STATS/,$d' 
Это имело для меня наибольшее значение, так что вы получите голос. Lennart Rolland 8 лет назад 0

Похожие вопросы