regex - Trying to colorize Apache http wire logs using sed -



regex - Trying to colorize Apache http wire logs using sed -

we have log files containing output apache http client. seeing output goes "over wire" , includes lines like:

<< http/1.1 200 request has succeeded

the chevrons '<<' indicate incoming, in contrast '>>' outgoing content. using 'tail -f' follow these logs entertaining plenty thought useful exercise utilize sed colorize output according whether input or output.

a simple test show mean:

echo '<< http/1.1 200 request has succeeded' | sed -r -e 's_<<_\x1b[31;1m&\x1b[0m_i' -e 's_>>_\x1b[32;1m&\x1b[0m_i'

for input, and

echo '>> http/1.1 200 request has succeeded' | sed -r -e 's_<<_\x1b[31;1m&\x1b[0m_i' -e 's_>>_\x1b[32;1m&\x1b[0m_i'

for output.

so far, good. descent regex madness began when occurred me more useful highlight http response codes , colorize them according class: greenish 2xx , reddish 5xx, example.

so far can match first digit in response code with: echo '<< http/1.1 200 request has succeeded' | sed -r -e 's_<<_\x1b[31;1m&\x1b[0m_i' -e 's_>>_\x1b[32;1m&\x1b[0m_i' -e 's_http[^[:alpha:]]*2\d*_\x1b[32;1m&\x1b[0m_g'

it colorizing to, << http/1.1 2. expectation http[^[:alpha:]]*2\d* match 'http', followed not alphabetic upto '2', followed number of digits. ideally utilize '{2}' rather '*' has same effect.

can regex guru point out mistake?

echo '<< http/1.1 200 request has succeeded' | \ sed -r -e 's_<<_\x1b[31;1m&\x1b[0m_;t http s_>>_\x1b[32;1m&\x1b[0m_ :http s_http[^[:alpha:]]\{1,\}2[0-9]\{1,\}\x1b[32;1m&\x1b[0m_g'

try this.

remove i end of << , >> line because never upper or lower case change * or {2} \{1,\} utilize + on gnu sed add optional t http :http because go farther , jump create faster on extension

try -u unbuffered improve on real stream

regex sed colorize

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -