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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
12-05-2012, 12:51 AM
|
#1
|
LQ Newbie
Registered: Oct 2012
Posts: 12
Rep:
|
[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
|
|
|
12-05-2012, 01:08 AM
|
#2
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by summer
...
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 occurrence s of "argument", "command" substrings.
|
|
|
12-05-2012, 01:14 AM
|
#3
|
LQ Newbie
Registered: Oct 2012
Posts: 12
Original Poster
Rep:
|
ARGV but really cant figure how to use it
|
|
|
12-05-2012, 01:17 AM
|
#4
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by summer
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 ?
|
|
|
12-05-2012, 07:31 AM
|
#5
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979
Rep:
|
Quote:
Originally Posted by summer
...
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
|
|
|
12-05-2012, 08:43 AM
|
#6
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by markush
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.
|
|
|
12-05-2012, 08:48 AM
|
#7
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979
Rep:
|
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 08:50 AM.
|
|
|
12-05-2012, 08:53 AM
|
#8
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by markush
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.
|
|
|
12-05-2012, 09:08 AM
|
#9
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by summer
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
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 09:10 AM.
|
|
|
12-05-2012, 10:36 AM
|
#10
|
LQ Newbie
Registered: Oct 2012
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by johnsfine
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..
|
|
|
12-05-2012, 10:47 AM
|
#11
|
LQ Newbie
Registered: Oct 2012
Posts: 12
Original Poster
Rep:
|
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...
|
|
|
12-05-2012, 10:55 AM
|
#12
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by summer
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
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 10:59 AM.
|
|
|
12-05-2012, 11:10 AM
|
#13
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by summer
...
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
?
|
|
|
12-05-2012, 11:40 AM
|
#14
|
LQ Newbie
Registered: Oct 2012
Posts: 12
Original Poster
Rep:
|
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
|
|
|
12-05-2012, 11:44 AM
|
#15
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979
Rep:
|
Quote:
Originally Posted by summer
...
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
|
|
|
All times are GMT -5. The time now is 05:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|