CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
edit multiple files in 1 script

 



jeffersno1
Novice

Dec 4, 2014, 12:33 AM

Post #1 of 2 (2020 views)
edit multiple files in 1 script Can't Post

Hi all,

I have some files that need editing and then re-saving and im struggling to get this working under 1 script.

I have multiple directories under directory /home/rrd_dirs, e.g sys1 / admin2 / res3 etc etc
Each directory stores lots of files and have a similar naming convention ie 01_name-time.csv

what i need the script to do is the following:

I need the first line removing in each file - this i have done below and works but only on 1 directory...
Then I need the name of the file adding to the beginning of each line in the file... This is where i'm stuck

so files look like this:
[root@system~]$ more rrd_dirs/sys1/01_allpass_kt-1417679587_1417681387.csv
epoch,datetime,passco1,passco2,errorco1,errorco2,buy_pass_reqco1,buy_pass_reqco2
1417679700,2014-12-04 07:55:00,0.00332511560480,0,0,0,0.00332511560480,0
1417680000,2014-12-04 08:00:00,0.0101359140450,0.00453220178690,0,0,0.0192003176180,0.0181288071470
1417680300,2014-12-04 08:05:00,0.00426892975960,0.00213446487980,0,0,0.0130850332450,0.00853785951920

and i need it to look like

[root@system~]$ more rrd_dirs/sys1/01_allpass_kt-1417679587_1417681387.csv
1417679700,01_allpass_kt,2014-12-04 07:55:00,0.00332511560480,0,0,0,0.00332511560480,0
1417680000,01_allpass_kt,2014-12-04 08:00:00,0.0101359140450,0.00453220178690,0,0,0.0192003176180,0.0181288071470
1417680300,01_allpass_kt,2014-12-04 08:05:00,0.00426892975960,0.00213446487980,0,0,0.0130850332450,0.00853785951920

The below only removes the 1st line in each file, can any suggest how i can go about adding part of the file name to the file???

Any help is much appreciated

Thanks

Code
#!/usr/bin/perl 

use Tie::File qw();

@csv = glob("/home/rrd_dirs/sys1/*.csv");

for my $filename (glob '/home/rrd_dirs/sys1/*.csv') {
tie my @file, 'Tie::File', $filename or die "Could not open $filename: $!";
shift @file;
}

for my $file ( @csv ) {

print "$file\n";

#system ('sed', '-i'i, '1d', '$file');
#system ("/bin/ls -l");

print "test - $file\n";
}



FishMonger
Veteran / Moderator

Dec 4, 2014, 6:52 AM

Post #2 of 2 (2013 views)
Re: [jeffersno1] edit multiple files in 1 script [In reply to] Can't Post

First, if you need to process files in a directory tree, then you should use either the File::Find or File::Find::Rule module to traverse the directory tree instead of using the glob function.
http://search.cpan.org/~rjbs/perl-5.18.4/lib/File/Find.pm
http://search.cpan.org/~rclamp/File-Find-Rule-0.33/lib/File/Find/Rule.pm

Using Tie::File to edit the files does add a little convenience, but also adds overhead (i.e., it's not real efficient). In this case I'd drop Tie::File and handle the opening/closing of the files myself.

As you process each line of the file you'll need to split it into its individual fields so that you can add the new field. For this step I'd use the Text::CSV_XS module.
http://search.cpan.org/~hmbrand/Text-CSV_XS-1.12/CSV_XS.pm

Before doing any of that, you need to add two pragmas; strict and warnings. They should be in EVERY script.

Here's how your script should start.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Find;
use Text::CSV_XS;

my $base_dir = '/home/rrd_dirs';

find(\&wanted, $base_dir);

exit;

###############################################################################

sub wanted {
return if $_ !~ /\.csv$/;
update_csv($File::Find::name);
}

sub update_csv {
my $file = shift;

}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives