Hi Monks,

programming since very long and knowing quite a number of languages (like C/C++, python, java, javascript, ...), I am however very new to perl. The need for püerl came form a quite complex bash script which needs also some data mangling. I started with awk (as usually) but came to one point where AWK became very heavy to handle it. So I wanted to give perl a try.

The script (in this part) needs to analyze SYSLOG logs. With the help of goole and some tutorials I came up with:

perl -n -e " /^(([a-zA-Z]+\s+[0-9]+\s+[0-9]+[:][0-9]+[:][0-9]+)\s+([0-9a-zA-Z-]+)\s ++([^:[]+)(\[.*?\])?\s*[:]\s*(?:([^:]*)[:])?\s*(.*))\$/ && ( \$m=(\$1, +\$2,\$3,\$4,\$5,\$6,\$7) ) && print \$3 " "$SYSLOG"

which works fine for testing and printing the system name of each syslog record. But the script is to be more complex (actually it is a bash library function), so I need to filter. Also as a test I did then:

perl -n -e " /^(([a-zA-Z]+\s+[0-9]+\s+[0-9]+[:][0-9]+[:][0-9]+)\s+([0-9a-zA-Z-]+)\s ++([^:[]+)(\[.*?\])?\s*[:]\s*(?:([^:]*)[:])?\s*(.*))\$/ && ( $3 =~ /^s +ystemname$/) && print \$3 " "$SYSLOG"

But this does not work. I understand that this is because $3 is overwritten by this new regex testing. So I tryed to assign to a variable like:

perl -n -e " /^(([a-zA-Z]+\s+[0-9]+\s+[0-9]+[:][0-9]+[:][0-9]+)\s+([0-9a-zA-Z-]+)\s ++([^:[]+)(\[.*?\])?\s*[:]\s*(?:([^:]*)[:])?\s*(.*))\$/ && ( \$m=(\$1, +\$2,\$3,\$4,\$5,\$6,\$7) ) && ( $3 =~ /^systemname$/) && print \$m[3 +]" "$SYSLOG"

but get only empty lines as well.

Questions:

  1. am I understanding correctly that this scrip timplicitely doe s$_ =~ before the initial regex ?
  2. do I really need the first capturing group for the full line if I also need to acces the full line ($_ seems not to wrok) ?
  3. How do I access the groups of the first regex at the end print ?

Even if you think there is a better way, I do also want to understand why this way does not work as expected. In the final script there will be dynamic testing (so there can be additonal && ( $x =~....) &&

Many thanka,
Gaston


In reply to Need advice in for perl use as awk replacement by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.