LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 03-05-2013, 03:17 PM   #1
kwoweiho
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Perl: how to access multiple directory with variables


I want to access multiple directories with the following structure and concatenate all the files from each director into one file:
/a/b/c/1
/a/b/c/2
....
/a/b/c/22

Here's my code in perl:

@chr=(1 .. 22);
$n=0;
while ($chr[$n]) {
$path = "/a/b/c/$chr[$n]";
opendir(DIR, $path) or die “Cannot open $path : $!”;
system (cat * >/a/b/c/$chr[$n].txt);
closedir(DIR);
$n++;
}

there's an error in the opendir line. and maybe more, can somebody help me out please?
Thanks!
David
 
Old 03-05-2013, 06:43 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Best practice is to start code with warnings & strict turned on
Code:
#!/usr/bin/perl -w
use strict;
It would also help if you showed the error msgs you're getting.
Incidentally, you should add error handling to closedir as well.

I'd guess at least one problem is you are opening a DIR to read, but you're not changing to that dir ( chdir() ), so the cat is working in the the wrong dir.
http://perldoc.perl.org/
 
Old 03-05-2013, 08:35 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,773

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
First time with Perl?

I think you are confusing opendir with chdir:

Code:
use warnings;
use strict;

for my $dir (1 .. 22) {
   chdir "/a/b/c/$dir";
   system("cat * >/a/b/c/$dir.txt");
}
 
Old 03-06-2013, 11:03 AM   #4
kwoweiho
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks guys, it works perfectly!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert multiple file in directory: ascii to hex in perl fad216 Programming 3 04-10-2011 01:47 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 06:59 PM
Multiple groups sharing directory access OnlyNewb Linux - General 2 11-20-2009 12:39 AM
Perl seems to think I have two INC variables. versaulis Linux - Software 3 11-18-2008 08:00 PM
Perl variables nbp Programming 2 11-02-2003 10:33 AM


All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration