J. Ribes J. Ribes - 1 month ago 13
Ruby Question

Regexp optimization when selecting text

I'm currently developing a small ruby script to get color syntax on my cisco logs (and later other manufacturers as ALU, Huawey, etc..)

Here is my algorithm:


  • Detecting a Cisco log syntax (
    ^DATE: %FACILITY-SEV-MNEMONIC: Message
    )

  • Pick text and adding to variables.

  • Print colored text on stdout.



Of course I will repeat this algo for each manufacturer later.

Here is the code for each line of log:

when /^(|\.|\*).*\s*.*\s(.*|.*\s.*): %.*-[1-6]-.*: .*$/ #Selecting IOS log as I expect.
/^(?<clock_sync>(\*|\.|))(?<d_month>\w{3})\s*(?<d_num>[0-9]*)\s*(?<d_hour>[0-9]{2}):(?<d_minute>[0-9]{2}):(?<d_second>[0-9]{2})(\.|)(?<d_msecond>([0-9]{3}|))(\s|)(?<host>(|.*)):\s(?<facility>.*)-(?<severity>.)-(?<mnemonic>.*):\s(?<message>.*)$/ =~ v # Get variables from log
puts ((dd_minute==d_minute) ? "| " : "+ " )+clock_sync.red+d_month.green+" "+d_num.green+" "+d_hour.green+":"+d_minute.green+":"+d_second.green+" |"+facility.bold+"-"+(severity.to_i>4? severity.bold.red : severity.to_i>2? severity.bold.brown : severity.bold)+"-"+mnemonic.bold+"| "+message
dd_minute = d_minute #Print Text


The execution doesn't satisfy me in terms of speed. I think it can be more efficient merging the first two lines but despite many tries I didn't find the correct syntax.

Maybe some of you could give me good advices. Thanks.

Answer

As I didn't find a good way to ask the question and after some research I realized that I was going the wrong way.

The main usage of the script has been redesigned around the "hhighlighter" bash+perl command that you can find here -> github.com/paoloantinori/hhighlighter Thanks everyone.