ProgrammingThis 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.
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.
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";
...
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.
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.
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.
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)?
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..
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.