A "split" question

M

mike

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
 
A

Anno Siegel

mike said:
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
 
J

Jürgen Exner

mike said:
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
 
W

Web Surfer

[This followup was posted to 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(/|/ , $_ );
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(/\|/ , $_ );
 
R

Robin

...
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top