By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,278 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,278 IT Pros & Developers. It's quick & easy.

how to read inputs from file for perl script?

P: 11
i am running my perl script as
"perl script.pl /root/sample/shadow .htpasswd"
now i need to pass these inputs in separate file
and read from that file.

for eg
content of file may be
/root/sample/shadow .htpasswd
/root/sample/shadow .htpasswd1
/root/sample/shadow .htpasswd2
/root/sample/shadow .htpasswd3

and my script is for comparing two files listed above.
Feb 15 '08 #1
Share this Question
Share on Google+
15 Replies


KevinADC
Expert 2.5K+
P: 4,059
I don't understand your question
Feb 15 '08 #2

nithinpes
Expert 100+
P: 410
If you are trying to compare two files, you can send the path of two/more files as arguments to the script separated by space directly :
Expand|Select|Wrap|Line Numbers
  1. perl script.pl "/root/sample/shadow .htpasswd" "/root/sample/shadow .htpasswd1" "/root/sample/shadow .htpasswd2"
  2.  
Inside thescript, you can access these files through @ARGV. For ex:
Expand|Select|Wrap|Line Numbers
  1.  open(F1,"$ARGV[0]") or die "failed to open first file";
  2.  open(F2,"$ARGV[1]") or die "failed to open second file";
  3.  
  4.   while(<F1>)
  5. {
  6.   ### read through first file line by line
  7. }
  8.  
  9.  
Feb 15 '08 #3

P: 11
If you are trying to compare two files, you can send the path of two/more files as arguments to the script separated by space directly :
Expand|Select|Wrap|Line Numbers
  1. perl script.pl "/root/sample/shadow .htpasswd" "/root/sample/shadow .htpasswd1" "/root/sample/shadow .htpasswd2"
  2.  
Inside thescript, you can access these files through @ARGV. For ex:
Expand|Select|Wrap|Line Numbers
  1.  open(F1,"$ARGV[0]") or die "failed to open first file";
  2.  open(F2,"$ARGV[1]") or die "failed to open second file";
  3.  
  4.   while(<F1>)
  5. {
  6.   ### read through first file line by line
  7. }
  8.  
  9.  
i need to give all the inputs in one file and read from that file.
u said to pass all arguments simultaneously, but i want to write those argument
in a file read from that.
Feb 15 '08 #4

nithinpes
Expert 100+
P: 410
i need to give all the inputs in one file and read from that file.
u said to pass all arguments simultaneously, but i want to write those argument
in a file read from that.
In that case, you have to run your original script from another script.
Write another script (e.g new.pl), and run the script as below:

perl new.pl paths.txt

where paths.txt is file containing filenames. Inside new.pl, you call the other script.

Expand|Select|Wrap|Line Numbers
  1.  
  2. while(<>)  ## read from file line by line(one file at a time)
  3. {
  4.   `perl script.pl "$_" ` ;  
  5. ## $_ is put between " " to include paths containg spaces in between. Not required otherwise
  6.  
There may be better ways of doing it. Hope, this is what you wanted.
Feb 15 '08 #5

P: 11
In that case, you have to run your original script from another script.
Write another script (e.g new.pl), and run the script as below:

perl new.pl paths.txt

where paths.txt is file containing filenames. Inside new.pl, you call the other script.

Expand|Select|Wrap|Line Numbers
  1.  
  2. while(<>)  ## read from file line by line(one file at a time)
  3. {
  4.   `perl script.pl "$_" ` ;  
  5. ## $_ is put between " " to include paths containg spaces in between. Not required otherwise
  6.  
There may be better ways of doing it. Hope, this is what you wanted.
i can't get it clearly.
As u said paths.txt i wrote like
/root/sss/shadow /root/.htpasswd
/root/sss/shadow /root/.htpasswd1
/root/sss/shadow /root/.htpasswd2

then new.pl
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) 
  3. {
  4. `perl script.pl "$_" ` ; 
  5. }
but i get error like
cannot open: No such file or directory in script.pl at line 2.
Feb 15 '08 #6

nithinpes
Expert 100+
P: 410
That may be because you are not chopping newline from the input,remove the trailing new line and try.
new.pl
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) 
  3. {
  4.  chomp;
  5. `perl script.pl "$_" ` ; 
  6. }
  7.  
  8.  
Feb 15 '08 #7

P: 11
That may be because you are not chopping newline from the input,remove the trailing new line and try.
new.pl
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) 
  3. {
  4.  chomp;
  5. `perl script.pl "$_" ` ; 
  6. }
  7.  
  8.  
again i am getting the same problem.
Feb 15 '08 #8

nithinpes
Expert 100+
P: 410
again i am getting the same problem.
Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl
Feb 15 '08 #9

P: 11
Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl

hi boss now i am getting the output correctly.
i forgot to remove " " in new.pl. after removed it i
got the output.
thnx for ur help.
Feb 15 '08 #10

nithinpes
Expert 100+
P: 410
Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl
In what format are those files in?How are you trying to open the file inside script.pl?
If you post your code, it would be helpful to identify..
Feb 15 '08 #11

nithinpes
Expert 100+
P: 410
Oops! We both posted message at the same time. Ok then.

Happy coding !!
Feb 15 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
I am still not even sure what the OP is doing, but it seems rather odd to shell out to perl from a perl script:

`perl script.pl "$_" ` ;
Feb 15 '08 #13

P: 11
I am still not even sure what the OP is doing, but it seems rather odd to shell out to perl from a perl script:

`perl script.pl "$_" ` ;
I got OP by removing " " in above statement. since there is no space in my
file path no need of putting " ".
Feb 16 '08 #14

KevinADC
Expert 2.5K+
P: 4,059
I got OP by removing " " in above statement. since there is no space in my
file path no need of putting " ".

Thats fine, but shelling out to a perl program from within a perl program seems totally unecessary.
Feb 16 '08 #15

nithinpes
Expert 100+
P: 410
I got OP by removing " " in above statement. since there is no space in my
file path no need of putting " ".
Senthilkumar,
I do agree with Kevin. Shelling out to a Perl script may be unnecessary in your scenario.
You can simplify it by including the while loop inside your original script and opening one file at a time, if that meets the objective. Another way is to convert your script to a subroutine and call the sub-routine for each iteration of while loop (each file).
You would have met your objective. But this way, script would be more efficient :)
Feb 18 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.