Your example even is giving syntax errors :
Code:
awk `{print$0 > "Ottawa"substr($1,7,4)".cw2"}`
You could probably accomplish what it looks like you want to do with something like:
Code:
find ./ -iname *.cw2 -exec awk '{print $0 > "Ottawa"substr($1,7,4)".cw2"}' {} \;
I do not think that awk does what you think that awk does. Why are you opening the file if you're reading nothing from or writing nothing to it? Or if it does, I'm confused about what you want to do.
Lets assume this:
filename.cw2
Code:
1234567890
abcdefghij
klmnopqrst
uvwxyzABCD
If you ran:
Code:
awk '{print $0 > "filename"substr($1,7,4)".cw2"}' filename.cw2
You'd get:
Code:
-rw-r--r-- 1 fred fred 11 2009-05-14 16:06 filename7890.cw2
-rw-r--r-- 1 fred fred 11 2009-05-14 16:06 filenameABCD.cw2
-rw-r--r-- 1 fred fred 44 2009-05-14 16:06 filename.cw2
-rw-r--r-- 1 fred fred 11 2009-05-14 16:06 filenameghij.cw2
-rw-r--r-- 1 fred fred 11 2009-05-14 16:06 filenameqrst.cw2
Each of which would contain the line their filename was based on. Is that what you're aiming for?
Code:
#!/usr/bin/perl
use warnings;
use File::Find;
$Start_dir = './';
die ">$Start_dir< is not a directory\n" if ! -d $Start_dir;
find (\&wanted, $Start_dir);
sub wanted {
if (-e $File::Find::name) { print "Found $File::Find::name\n"; }
else { print "Didn't find $File::Find::name\n"; }
if ($File::Find::name =~ m/.+\.[Cc][Ww][2]$/) {
print "Processing Name+Path: $File::Find::name Name: $_ Directory: $File::Find::dir\n";
my $ext=".cw2";
my $filename=$_;
$filename =~ s/$ext//g;
system("awk \'\{print \$0 > \"$File::Find::dir/$filename\"substr(\$1,7,4)\"$ext\"\}\' $File::Find::name");
}
}
If so then something like the above might work, mind I didn't test to see if this would run so it might need less or more escaped *shrug*... but that should be close enough to get you going in the right way.
You could do the same in perl by opening the file, reading the contents in, parsing them and writing them out based on the parsed contents. Really shouldn't be using a system call like that if you can avoid it.
Code:
sub wanted {
if (-e $File::Find::name) { print "Found $File::Find::name\n"; }
else { print "Didn't find $File::Find::name\n"; }
if ($File::Find::name =~ m/.+\.[Cc][Ww][2]$/) {
print "Processing Name+Path: $File::Find::name Name: $_ Directory: $File::Find::dir\n";
chdir($File::Find::dir);
my $ext=".cw2";
my $filename=$_;
$filename =~ s/$ext//g;
open(CW2,$File::Find::file);
while(<CW2>) {
my $value = substr($_,7,4);
open(CW2A,">$File::Find::dir/$filename$value$ext");
print CW2A $_;
close(CW2A);
}
close(CW2);
}
}
(again untested)
Could also just do this whole mess in bash using find too.