Searching in a line

L

lerameur

Hello all,

I want to do a search in a log file. each line has the following
pattern:
34,56,,9,0,0,,,,65;
So there 10 section. How do I parse through different sections?
meaning I want section 4, which is the number 9

thanks
ken
 
I

it_says_BALLS_on_your forehead

Hello all,

I want to do a search in a log file. each line has the following
pattern:
34,56,,9,0,0,,,,65;
So there 10 section. How do I parse through different sections?
meaning I want section 4, which is the number 9

if every possible value within a 'section' cannot contain a comma ',',
then you can just split on a comma, and take the 4th section.

my $log = 'log.txt';
open my $fh, '<', $log or die "can't open '$log': $!\n";
while ( <$fh> ) {
my $val = (split /,/)[3];
print $val, "\n";
}
close $fh;
 
J

Jürgen Exner

lerameur said:
Hello all,

I want to do a search in a log file. each line has the following
pattern:
34,56,,9,0,0,,,,65;
So there 10 section. How do I parse through different sections?

perldoc -f split
meaning I want section 4, which is the number 9

$sec4 = split($value)[3]

jue
 
L

lerameur

lerameur said:
Hello all,
I want to do a search in a log file. each line has the following
pattern:
34,56,,9,0,0,,,,65;
So there 10 section. How do I parse through different sections?

perldoc -f split
meaning I want section 4, which is the number 9

$sec4 = split($value)[3]

jue


Hi
either way I am getting these error messages:
../spam2: my: command not found
../spam2: line 6: unexpected EOF while looking for matching `"'
../spam2: line 10: syntax error: unexpected end of file

ken
 
J

Jürgen Exner

lerameur said:
lerameur said:
I want to do a search in a log file. each line has the following
pattern:
34,56,,9,0,0,,,,65;
So there 10 section. How do I parse through different sections?

perldoc -f split
meaning I want section 4, which is the number 9

$sec4 = split($value)[3]

either way I am getting these error messages:
./spam2: my: command not found
./spam2: line 6: unexpected EOF while looking for matching `"'
./spam2: line 10: syntax error: unexpected end of file

Ok, ok, that split() syntax is not correct, I forgot to specify the pattern.
If you had included a self-contained minimal program then I could have
tested my suggestion and that would not have happened.

As for the other error messages: you didn't show us your program, therefore
there is no way for us to see what is wrong. Have you read the posting
guidelines that are posted here frequently?

jue
 
J

Jürgen Exner

Ok, ok, that split() syntax is not correct, I forgot to specify the
pattern. If you had included a self-contained minimal program then I
could have tested my suggestion and that would not have happened.

Here's the spoonfed version:

my $foo = '34,56,,9,0,0,,,,65';
my $bar = (split(/,/,$foo))[3];
print $bar;

jue
 
P

Peter Makholm

lerameur said:
Hi
either way I am getting these error messages:
./spam2: my: command not found
./spam2: line 6: unexpected EOF while looking for matching `"'
./spam2: line 10: syntax error: unexpected end of file

Looks like you're trying to use bash af perl interpreter. That
wouldn't work. Did you forgot the #!-line?

//Makholm
 
L

lerameur

Looks like you're trying to use bash af perl interpreter. That
wouldn't work. Did you forgot the #!-line?

//Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic
 
L

lerameur

Looks like you're trying to use bash af perl interpreter. That
wouldn't work. Did you forgot the #!-line?

//Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic
 
L

lerameur

Looks like you're trying to use bash af perl interpreter. That
wouldn't work. Did you forgot the #!-line?

//Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic
 
J

Jürgen Exner

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue
 
L

lerameur

Looks like you're trying to use bash af perl interpreter. That
wouldn't work. Did you forgot the #!-line?

//Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 
L

lerameur

lerameur said:
my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {
my $val =(split( /,/,$log))[3]
print $val, "\n";
}
close $fh;
getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.
New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top