Text editing trivia

Harvey

Registered
Hey guys, I have a little delima.

Here is the objective.... The file, BOB12.csv has the following contents.
I want to change the flie to be like... the second group below.

34,344,34444
44,444,44444
...
...

Change to...

34,344,34444,BOB12.csv
44,444,44444,BOB12.csv
...BOB12.csv

and on and on. Lots of lines in the files.

See what I mean?

Problem is, there are a LOT of files... all formatted the same, with different file names. Need to stick the apporiate file name on the end of all lines in that file.

Anyone have -any- idea how to do this? I woudl write a macro in a text editor, but, I can't access the filename from the editor so easily.... grrr......
 
You could probably do it with Applescript. You could also probably write an MS-Word (VBA) script to do it.

I'd give you some code, but although I've done a fair amount of playing with applescript, for some reason its a language that I just can't keep fresh in my head. I always have to look at previous things I've written to refresh my memory. ANd alas, I don't have access to my Mac right now...
 
in BBedit, or BBedit lite, or another good code editor (like SubEthaEdit) do a find and replace. Replace either \n or \r (whichever works) with BOB12.csv\n or BOB12.csv\r.

-JARinteractive

edit: I just reread your post and realized what you meant...theoretically an applescript could make an editor do this to multiple files, though.
 
Perl is your friend! copy this into your favorite text editor, chmod it to 755, give it a directory, and it'll do it for every file in that directory!

-------
#!/usr/bin/perl -w

use File::Find;
use File::Basename;

find(\&addDocName, $ARGV[0]);

sub addDocName{
if (/\.csv$/) {
my $fpathname = $File::Find::name;
my $fname = basename($fpathname);

open(IFH,"<$fname") or die ("Couldn't open $fpathname for Reading.\n");
my @lines = <IFH>;
close(IFH);

open(OFH,">$fname") or die ("Couldn't open $fpathname for Writing.\n");
foreach ( @lines ){
if( ! /$fname$/ ) {
s/(.*)/$1,$fname/;
}
print OFH;
}
close(OFH);

print "Updated $fpathname.\n";
}

}
 
Back
Top