LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2012, 11:51 PM   #1
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Rep: Reputation: Disabled
[URGENT] Perl programming help


i need to output the text of lines 1,4,5 to standard error stream.lines 2 & 3 to standard output.at end i need to output how many errors it got.
example of a log file may be:
ERROR - no error found.
I found ERROR in line 9.
Nothing to do.
ERROR not excepted.
ERROR: your file is not available.
several input files might be called like this
cat logfile1 logfile2 | myprogram.pl

here is my code:

use strict;
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}

my $line;
my $count = 0;

open (LOG,"logdata") or die "Unable to open logfile";

while($line = <LOG>){
$line = trim($line);
if($line !~ /^ERROR/ ){
print STDOUT "$line.\n";
next;
}
else{
print STDERR "$line.\n";
$count++;
}

}
print "There were $count errors.\n";
close(LOG);

i dont know how to take the filename from argument
 
Old 12-05-2012, 12:08 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by summer View Post
...
i dont know how to take the filename from argument
Which part of http://perldoc.perl.org/ have you already read ? If none, I suggest to start from the links in the leftmost column.

Regarding "argument" - if you mean command line arguments, start from http://perldoc.perl.org/perlvar.html and look for occurrences of "argument", "command" substrings.
 
Old 12-05-2012, 12:14 AM   #3
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
ARGV but really cant figure how to use it
 
Old 12-05-2012, 12:17 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by summer View Post
ARGV but really cant figure how to use it

@ARGV is an array. So what is exactly the problem using the array - compared to other arrays ?
 
Old 12-05-2012, 06:31 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by summer View Post
...
several input files might be called like this
Quote:
cat logfile1 logfile2 | myprogram.pl
...
Quote:
i dont know how to take the filename from argument
This are different subjects.
Your example with cat would mean that the data come from a pipe which has nothing to do with arguments

Filenames as arguments require indeed the @ARGV array.

Hint: in the first case the perlscript "doesn't know" about the filenames, in the second case the script handles the filenames.

Markus
 
Old 12-05-2012, 07:43 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markush View Post
This are different subjects.
Your example with cat would mean that the data come from a pipe which has nothing to do with arguments

Filenames as arguments require indeed the @ARGV array.

Hint: in the first case the perlscript "doesn't know" about the filenames, in the second case the script handles the filenames.

Markus
Regardless of Perl in case of

Code:
foo <some_files> | bar
'bar' can not know what <some_files> are - unless this info is explicitly passed through STDOUT by 'foo'.

...

Well, probably it can - in a very unportable and convoluted way, looking into process table, etc.
 
Old 12-05-2012, 07:48 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
My intention was to point the OP to the difference between @ARGV and <STDIN>. He would be better off to consider before writing the script how the logfile(s) should be passed to it.

Markus

Last edited by markush; 12-05-2012 at 07:50 AM.
 
Old 12-05-2012, 07:53 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markush View Post
My intention was to point the OP to the difference between @ARGV and <STDIN>. He would be better off to consider before writing the script how the logfile(s) should be passed to it.

Markus
It's a good point.
 
Old 12-05-2012, 08:08 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by summer View Post
several input files might be called like this
cat logfile1 logfile2 | myprogram.pl
Obviously your program is required to handle the case in which no arguments are provided and the input comes through stdin.

Quote:
Originally Posted by summer View Post
i dont know how to take the filename from argument
Some program need to also handle the case in which the arguments provide the input filename. It is easy enough to check whether you have an input filename specified as an argument and if not, default to taking input from stdin.

But start by making sure you understand the assignment. Are you only required to handle input from stdin, or are you required to handle input either way (from stdin or from a specified file)?

Last edited by johnsfine; 12-05-2012 at 08:10 AM.
 
Old 12-05-2012, 09:36 AM   #10
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post

But start by making sure you understand the assignment. Are you only required to handle input from stdin, or are you required to handle input either way (from stdin or from a specified file)?
i am required to handle input from stdin and not a specified file. i have provided the code for a specified file but it will not be a specified file.it will be the files that the user will provide..
 
Old 12-05-2012, 09:47 AM   #11
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
I updated my code to take multiple input

Here is my updated code
#!/usr/bin/perl -w
use strict;
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}

FILE:foreach(@ARGV){

my $line;
my $count=0;

open (FILE,$_) or die "Unable to open logfile";

while($line = <FILE>){
$line = trim($line);
if($line !~ /^ERROR/ ){
print STDOUT "$line.\n";
next;
}
else{
print STDERR "$line.\n";
$count++;
}


}
print "There were $count errors.\n";
close(FILE);
}

But When I execute, cat logfile1 logfile2 | myprogram.pl

I am getting error
-bash: zprogram11.pl: command not found

Thanks for all the help...
 
Old 12-05-2012, 09:55 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by summer View Post
i am required to handle input from stdin and not a specified file.
You say that, but then you act like you don't know it.
I think you should reread some of the answers you already received.

There is nothing to get from ARGV. Your program is not supposed to know or care about the filename that was the source for stdin.

You don't open any file to read stdin. STDIN is effectively a file that is already open when your program starts. You just pull text from <STDIN>

Quote:
Originally Posted by summer View Post
I updated my code to take multiple input
stdin is one input. The fact that it might have come from multiple files through cat is invisible to your program.

Quote:
But When I execute, cat logfile1 logfile2 | myprogram.pl

I am getting error
-bash: zprogram11.pl: command not found
I have no clue why that command would give that error. I'm not an expert in either bash or perl, so maybe I'm misunderstanding some key detail, but it looks to me like you are giving us incorrect or incomplete information.

Last edited by johnsfine; 12-05-2012 at 09:59 AM.
 
Old 12-05-2012, 10:10 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by summer View Post
...
I am getting error
-bash: zprogram11.pl: command not found

Thanks for all the help...
Is 'zprogram11.pl' executable ? Is current working directory in $PATH ? Why won't you call your script as

Code:
./zprogram11.pl
?
 
Old 12-05-2012, 10:40 AM   #14
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
over here myprogram.pl = zprogram11.pl

I can call ./zprogram11.pl but i need to process multiple log file. That is why i need to pass those file name.

I need to pass it like this

cat logFile1 logFile2 | zprogram11.pl
 
Old 12-05-2012, 10:44 AM   #15
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by summer View Post
...
cat logFile1 logFile2 | zprogram11.pl
Normally when your homefolder is not in $PATH, you will have to execute
Code:
cat logFile1 logFile2 | ./zprogram11.pl
Markus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration