Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Need advice in for perl use as awk replacement

by BillKSmith (Monsignor)
on Sep 21, 2020 at 22:35 UTC ( [id://11122047]=note: print w/replies, xml ) Need Help??


in reply to Need advice in for perl use as awk replacement

The -n flag in your code creates an implied while loop which reads from STDIN. For the purpose of this post, I am using an explicit while loop. I have redirected STDIN to a memory file which contains the example data from SyslogScan::SyslogEntry. I saved your regex in a variable to separate the matching and processing issues. You had the right idea about saving the match values, but you should use an array. It is both easier and clearer to use 'eq' rather than an regex to match a constant pattern. Note that at the end of the loop, $_ still contains the original line and @m contains all the original matches. I recommend that you write and debug your complete logic in this form. If necessary, you can rewrite it as the terse one-liner.
use strict; use warnings; use feature 'state'; my $syslog_names = \"Jun 13 02:32:27 satellife in.identd[25994]: connect from mail.mi +ssouri.edu\n"; close STDIN; open STDIN, '<', $syslog_names or die "$!"; while (<>) { state $systemname = 'in.identd'; state $regex = qr/^ ( ([a-zA-Z]+\s+[0-9]+ \s+ [0-9]+[:][0-9]+[:][0-9]+) \s+ ([0-9a-zA-Z-]+) \s ([^:[]+) (\[.*?\])? \s* [:] \s* (?: ([^:]*)[:])? \s* (.*) ) $ /x; if (m/$regex/) { my @m = ($1, $2, $3, $4, $5, $6, $7); print "$m[3]\n$_\n" if $m[3] eq "$systemname"; } }

OUTPUT:

in.identd Jun 13 02:32:27 satellife in.identd[25994]: connect from mail.missouri +.edu
Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122047]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found