Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:
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
|
---|