Operating System - HP-UX
1754320 Members
3018 Online
108813 Solutions
New Discussion юеВ

how to get whole data from the file in perl?

 
SOLVED
Go to solution
diwakar_4
Frequent Advisor

how to get whole data from the file in perl?

Hi All,

I have file like :

input.txt: contains list of dir.
JN001
JN002
JN003
JN004
******************************************
my $data_file = '/usr/symology/perl_get_put/data.txt';
# Open the file for reading.
open DATA, "$data_file" or die "can't open $data_file $!";
my @array_of_data = ;
close (DATA);
foreach $file(@array_of_data)
{
print "Dir listing is as :$file \n"
}

Output is :
Sucessfully cpoied files: JN001

Sucessfully cpoied files: JN002

Sucessfully cpoied files: JN003

Sucessfully cpoied files: JN004

}


After every print it is printing one blank line. Can some one suggest what is wrong?

Thanks
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: how to get whole data from the file in perl?

Hi:

You need to 'chomp' your lines or drop the "\n" in your print().

Consider:

# perl -e 'chomp(@lines=<>);for $line (@lines) {print "$line\n"}' /etc/hosts

or:

# perl -e '@lines=<>;for $line (@lines) {print $line}' /etc/hosts

Regards!

...JRF...
diwakar_4
Frequent Advisor

Re: how to get whole data from the file in perl?

Thanks , It resolved my problem. Actually i am trying to connect to FP server and trying ot get files from different dir .

My code is working fine if i hard code the dirs in foreach loop like:

use Net::FTP;
use Cwd ;
use File::Basename ;
use POSIX ":sys_wait_h" ;

-->After connecting successfully i need to go each dir...

foreach my $name ('/Insight-Interface/Permits/JN001', '/Insight-Interface/Per
mits/JN002') {
$ftp->cwd("$name") or die "Can not change dir $name";
Printf" successfully change dir"
}

--> This code is fine. But i want to pass these dir path from input file like..
open DATA, "$data_file" or die "can't open $data_file $!";
my @array_of_data = ;
close (DATA);
foreach my $name (@array_of_data) {
$ftp->cwd("$name") or die "Can not change dir $name";
Printf" successfully change dir"
}
}
--> In this case it gives die error meesage Can not connect.

Can you please help me what is wrong if same structure i am passing from file?
Thanks
James R. Ferguson
Acclaimed Contributor
Solution

Re: how to get whole data from the file in perl?

Hi (again):

> In this case it gives die error meesage Can not connect.

Yes, of course. You need to re-read my first ressponse to you. You need to 'chomp' the newlines away when you stuff your array.

...
open(DATA, '<', $data_file) or die "can't open $data_file $!";

chomp( my @array_of_data = );
close (DATA);

foreach my $name (@array_of_data) {
$ftp->cwd($name) or die "Can not change dir $name";
print "successfully changed dir $name\n"
}
...

I also fixed a few other typos.

Regards!

...JRF...
diwakar_4
Frequent Advisor

Re: how to get whole data from the file in perl?

Hi James,

Good Morning,
Appologies, i was in leave. Thanks it looks fine. But i still have problem. In my FTP server dir strucrute is like..

root/data/JN001
root/data/JN002
I have to CWD to each dir one by one. This input i want to give through input file. When i keeping inputs in data file like :

data.txt:
*************************************
root/data/JN001
root/data/JN002
*************************************
When i am passing these inputs to foreach loop it is not able to change the dir.
Not sure whether we should quote the path in file. Can you please suggect?

Thanks

James R. Ferguson
Acclaimed Contributor

Re: how to get whole data from the file in perl?

Hi (again) diwakar:

> I have to CWD to each dir one by one. This input i want to give through input file. When i keeping inputs in data file like :

First, you need to specify absolute paths. The first change of directory sets your current working path to '/root/data/JN001'. The next, in your example, attempts to change the path to '/root/data/JN001/root/data/JN002'.

> When i am passing these inputs to foreach loop it is not able to change the dir.
Not sure whether we should quote the path in file.

This is explained above.

Regards!

...JRF...
diwakar_4
Frequent Advisor

Re: how to get whole data from the file in perl?

Many thanks James,

You are correct. Now i am passing absolute path from input file and paths are not quoted like

/root/JN001/
/root/JN002/

It is working now.

Thanks