[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Perl

Hi,

I have a directory which is generated at run time which looks something like this..

"2011 03 23 11 41 AM" which has a log file "log file.log". To read the "log file.log" I am doing the following in my perl script..

$out = `ls /home/anand/exinda/automate/tests/SwiftTest//tc001246/Results`;
print "$out";
# Adding double quotes before the directory names because of the space delimiter problem in linux
$o = `ls \"/home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/$out\"`;
print $o;

But i get the followin error..

anand@anand-OptiPlex-980:~/exinda/automate/tests/SwiftTest/tc001246$ perl test.pl
5_4_2011 9-59-54 AM
ls: cannot access /home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/5_4_2011 9-59-54 AM
: No such file or directory

I am not sure how to achieve this.. Please help.

Regards,
Anand.
0
anand_sridharan
Asked:
anand_sridharan
  • 3
  • 3
  • 3
  • +1
1 Solution
 
farzanjCommented:
Try this

my $out = qx {ls /home/anand/exinda/automate/tests/SwiftTest//tc001246/Results }
0
 
anand_sridharanAuthor Commented:
Hi,

You mean like this..

$out = qx{ls /home/anand/exinda/automate/tests/SwiftTest//tc001246/Results};
print $out
$o = qx{ls //home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/$out};
print $o;

The result is still the same

ls: cannot access //home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/5_4_2011: No such file or directory
ls: cannot access 9-59-54: No such file or directory
ls: cannot access AM: No such file or directory


-A-
0
 
farzanjCommented:
Did you run the command on the command line?

ls /home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/5_4_2011
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
anand_sridharanAuthor Commented:
Actually the problem is there is a file "5_4_2011 9-59-54 AM"..
When I do ls from linux i can do this from command line

ls "//home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/5_4_2011 9-59-54 AM" and this works perfectly..

I want to achieve the same through perl to list the log file under this directory. Because Linux treats " " as delimeter between dirs and file names I have to issue the command through a quoted string as was done from the command line.

-A-
0
 
farzanjCommented:
Try this
qx {ls /home/anand/exinda/automate/tests/SwiftTest//tc001246/Results/5_4_2011\ 9-59-54\ AM};
0
 
ozoCommented:
try
chomp $out;
0
 
wilcoxonCommented:
Personally, I'd do it within perl rather than shelling out multiple times...

It should be more efficient and is much more flexible.
#!/usr/local/bin/perl

use strict;
use warnings;

my $dir = '/home/anand/exinda/automate/tests/SwiftTest//tc001246/Results';

opendir DIR, $dir or die "could not open $dir dir: $!";
my @dirs = grep { -d $_ } readdir DIR;
closedir DIR;

foreach my $d (@dirs) {
    opendir DIR, "$dir/$d" or die "could not open $dir/$d dir: $!";
    my @files = readdir DIR;
    closedir DIR;
    print "found the files:\n", join("\n", @files), "\n";
}

Open in new window

0
 
anand_sridharanAuthor Commented:
Actually I had to change the code slightly for this to work..

#!/usr/local/bin/perl

use strict;
use warnings;

my $dir = '/home/anand/exinda/automate/tests/SwiftTest//tc001246/Automation/Results';

opendir DIR, $dir or die "could not open $dir dir: $!";
#my @dirs = grep { -d $_ } readdir DIR;
my @dirs = readdir DIR;
closedir DIR;

foreach my $d((@dirs)) {
    if ($d eq ".." || $d eq ".") {
        next;
    }

    opendir DIR, "$dir/$d" or die "could not open $dir/$d dir: $!";
    my @files = readdir DIR;
    closedir DIR;
    print "found the files:\n", join("\n", @files), "\n";
}
~
0
 
wilcoxonCommented:
Ah.  Yep.  I always forget that "grep { -d $_ } readdir" picks up . and ..

You could also write the next as:

next if ($d =~ m{^\.+$});

or

next if ($d eq '.' or $d eq '..');

However, what you have is fine and works.
0
 
wilcoxonCommented:
Hmm.  I just noticed you commented out the grep version of readdir.

You could try:

my @dirs = grep { -d "$dir/$_" } readdir DIR;

which should fix it I think.  Alternately, you could skip the next line by including it in the grep:

my @dirs = grep { -d "$dir/$_" and not m{^\.+$} } readdir DIR;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now