A "split" question

Discussion in 'Perl Misc' started by mike, Jan 20, 2004.

  1. mike

    mike Guest

    hi

    i have a log file with contents like (separate by | ):

    19Jan2004| 0:00:45||log|accept||eri0|inbound|tcp|||http|57135|48|74|||52509|http|||||
    19Jan2004| 0:00:45||log|accept||eri0|inbound|tcp|||http|4386|48|11|||4386|http|||||
    19Jan2004| 0:00:45||log|accept||eri0|inbound|tcp|||http|4387|48|11|||4387|http|||||


    I wrote a perl script to split the fields according to "|"
    ....
    while ( <FILE> )
    {
    chomp($_);
    my @lines = split(/|/ , $_ );
    print "The lines are : @lines\n";

    }
    close(FILE);
    ....


    The results are:

    The lines are : 1 9 J a n 2 0 0 4 | 0 : 0 0 : 4 5 | | l o g | a c c
    e p t | | q f e 2 | i n b o u n d | t c p | | | h t t p | 5 7 1 3 5 |
    4 8 | 7 4 | | | 5 2 5 0 9 | h t t p | | | | |
    The lines are : 1 9 J a n 2 0 0 4 | 0 : 0 0 : 4 5 | | l o g | a c c
    e p t | | q f e 1 | i n b o u n d | t c p | | | h t t p | 4 3 8 6 | 4
    8 | 1 1 | | | 4 3 8 6 | h t t p | | | | |
    The lines are : 1 9 J a n 2 0 0 4 | 0 : 0 0 : 4 5 | | l o g | a c c
    e p t | | q f e 1 | i n b o u n d | t c p | | | h t t p | 4 3 8 7 | 4
    8 | 1 1 | | | 4 3 8 7 | h t t p | | | | |


    "Split" did not split the fields up.. what is wrong with my code.
    thanks
     
    mike, Jan 20, 2004
    #1
    1. Advertising

  2. mike

    Anno Siegel Guest

    mike <> wrote in comp.lang.perl.misc:
    > hi
    >
    > i have a log file with contents like (separate by | ):
    >
    > 19Jan2004|
    > 0:00:45||log|accept||eri0|inbound|tcp|||http|57135|48|74|||52509|http|||||
    > 19Jan2004|
    > 0:00:45||log|accept||eri0|inbound|tcp|||http|4386|48|11|||4386|http|||||
    > 19Jan2004|
    > 0:00:45||log|accept||eri0|inbound|tcp|||http|4387|48|11|||4387|http|||||
    >
    >
    > I wrote a perl script to split the fields according to "|"
    > ...
    > while ( <FILE> )
    > {
    > chomp($_);
    > my @lines = split(/|/ , $_ );

    ^
    [snip]

    In a regex, "|" is an operator, so it must be quoted to be taken literally.

    my @lines = split(/\|/ , $_ );

    Anno
     
    Anno Siegel, Jan 20, 2004
    #2
    1. Advertising

  3. mike wrote:
    > I wrote a perl script to split the fields according to "|"
    > ...
    > while ( <FILE> )
    > {
    > chomp($_);
    > my @lines = split(/|/ , $_ );


    From "perldoc perlre":
    In particular the following metacharacters have their standard
    *egrep*-ish meanings:

    [...]
    | Alternation

    In other words: your RE matches (and therefore split tries to split at)
    "nothing or nothing".
    If you want to match a literal vertical bar then you need to escape the bar
    character:
    /\|/

    jue
     
    Jürgen Exner, Jan 20, 2004
    #3
  4. mike

    Web Surfer Guest

    [This followup was posted to comp.lang.perl.misc]

    In article <>,
    says...
    > hi
    >
    > i have a log file with contents like (separate by | ):
    >
    > 19Jan2004| 0:00:45||log|accept||eri0|inbound|tcp|||http|57135|48|74|||52509|http|||||
    > 19Jan2004| 0:00:45||log|accept||eri0|inbound|tcp|||http|4386|48|11|||4386|http|||||
    > 19Jan2004| 0:00:45||log|accept||eri0|inbound|tcp|||http|4387|48|11|||4387|http|||||
    >
    >
    > I wrote a perl script to split the fields according to "|"
    > ...
    > while ( <FILE> )
    > {
    > chomp($_);
    > my @lines = split(/|/ , $_ );
    > print "The lines are : @lines\n";
    >
    > }
    > close(FILE);
    > ...
    >
    >
    > The results are:
    >
    > The lines are : 1 9 J a n 2 0 0 4 | 0 : 0 0 : 4 5 | | l o g | a c c
    > e p t | | q f e 2 | i n b o u n d | t c p | | | h t t p | 5 7 1 3 5 |
    > 4 8 | 7 4 | | | 5 2 5 0 9 | h t t p | | | | |
    > The lines are : 1 9 J a n 2 0 0 4 | 0 : 0 0 : 4 5 | | l o g | a c c
    > e p t | | q f e 1 | i n b o u n d | t c p | | | h t t p | 4 3 8 6 | 4
    > 8 | 1 1 | | | 4 3 8 6 | h t t p | | | | |
    > The lines are : 1 9 J a n 2 0 0 4 | 0 : 0 0 : 4 5 | | l o g | a c c
    > e p t | | q f e 1 | i n b o u n d | t c p | | | h t t p | 4 3 8 7 | 4
    > 8 | 1 1 | | | 4 3 8 7 | h t t p | | | | |
    >
    >
    > "Split" did not split the fields up.. what is wrong with my code.
    > thanks


    The "|" is a "special character" as far as regular expressions are
    concerned (commonly refered to as a metacharacter). You need to
    "escape" the "|" as follows :

    my @lines = split(/\|/ , $_ );
     
    Web Surfer, Jan 20, 2004
    #4
  5. mike

    Robin Guest

    > ...
    > while ( <FILE> )
    > {
    > chomp($_);
    > my @lines = split(/|/ , $_ );
    > print "The lines are : @lines\n";
    >
    > }
    > close(FILE);


    You have to escape it, with split (/\|/, $_);
    Because "|" is the or atom qualifier.
    peace- Robin
     
    Robin, Jan 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    492
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    742
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    241
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    280
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    714
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page