Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

split delimiters II

by bailybj (Initiate)
on Mar 27, 2001 at 02:22 UTC ( [id://67356]=perlquestion: print w/replies, xml ) Need Help??

bailybj has asked for the wisdom of the Perl Monks concerning the following question:

As I have never posed questions to the Perl Gods, I wasn't what you needed here is the data:
The date time> [21]Alert Started (34897), Status: etc... the code reads: ($date_stamp,$event,$alert_id,$status)=split(/>/); but that only breaks off the first part. Now I need to go further: 1:) > works for the date_stamp 2:) [ is needed for the [21] Alert Started (The [21] is not always the same but this little phrase always ends in "ed". 3:)(34897)is the alert_id but can vary from 1-5digits 4:) , will work for Status. How do I use [ and ) for split delimiters and how do I list more than a one delimiter split?
Help Please????

Edit 2001-03-26 by tye to add <code> tags

Replies are listed 'Best First'.
(jeffa) Re: split delimiters II
by jeffa (Bishop) on Mar 27, 2001 at 02:57 UTC
    Unless you post a complete example of the line to be parsed, we cannot help you. Since I am a nice guy, though, I will explain why you need more than split for this problem.

    The better solution would be to use the m// operator and the grouping variables: $1, $2, etc. I'll explain by parsing an entry from an Apache web server access log

    $line = '127.0.0.1 - - [26/Mar/2001:16:01:07 -0500] "GET /stuff/ HTTP/ +1.0" 200 11874'
    Each entry is seperated by dashes, brackets, or quotes - but since we know the general layout, we can write a regualar expression that is general enough to parse each line, but specific enough to get the data we want - just the IP of the referrer, the date stamp and the requested document ( along with request type)
    use strict; my ($ip,$date,$method,$file,$header,$status,$pid) = $line =~ /^([\d.]+) # $id = ip quad \s*-\s*-\s* # skip over these \[(.*?)\]\s" # $date = everything between the brackets (\w+)\s* # $method = the method, usually GET or POST ([^\s]+)\s* # $file = everything UP TO the next white space (.*)"\s* # $header = everything UP TO the next double quote (\d+)\s* # $status = digits between spaces (\d+)\s*$/x; # $pid = last set of digits print "$ip\n$date\n$method\n$file\n$status\n$pid\n";
    By no means I am an master of regular expressions, the ones I chose just happen to work - there are better ways then using .* - but a little badness won't kill ya' :)

    Big Thanks to Albannach.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: split delimiters II
by Anonymous Monk on Mar 27, 2001 at 02:34 UTC
    Enclose code in a <code> tag.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found