LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to Use AWK Command in Perl Script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-awk-command-in-perl-script-726018/)

briana.paige 05-14-2009 12:28 PM

How to Use AWK Command in Perl Script
 
Hi,

I have numerous folders with *.cw2 files in them. I want my code to find all the *.cw2 files in all the folders and run the following awk command:

awk `{print$0 > '$File::Find::name' substr($1,7,4)".cw2"}`

From the bash window, awk `{print$0 > "Ottawa"substr($1,7,4)".cw2"}` does what I want, i.e., reads in the file name from each line of the data and separates the lines into seperate files corresponding to the substr().

Right now the code returns the list of all the files in the directory as well as opens the *.cw2 files but I can't get it to use my awk command to separate all the *.cw2 files into their own files.

I also want the beginning of my file name to be the appropriate filename found using $File::Find::name rather than "Ottawa" and I'm not sure I'm going about that the right way?

I appreciate any help. Thanks!

Code:

#!/usr/bin/perl

use warnings;
use File::Find;

$Start_dir = '/cygdrive/c/Documents and Settings/Briana/Desktop/CWEEDS-Updates';

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]$/)
          {
            #Open CW2 file.
            open (CW2FILE,$File::Find::name ) or die "could not open '$InputFile'  $!\n";
            print "Processing $File::Find::name\n";
            #awk `{print$0 > $File::Find::name substr($1,7,4)".cw2"}`
            }
}


rweaver 05-14-2009 02:41 PM

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.

chrism01 05-14-2009 06:21 PM

I certainly agree that it its make more sense to do the whole thing in Perl. I also agree its not entirely clear what you want.
As well as

use warnings;

best practice is to also use

use strict;

then do

perl -wc your_perl_prog.pl

which will check the syntax without actually running the program.

briana.paige 05-15-2009 09:06 AM

Thank-you both for your input. I think this will indeed point me in the right direction. I apologise for the lack of clarity regarding my problem but I think your input has helped!
Thanks!

rweaver 05-15-2009 11:48 AM

No problem, if you need additional help please feel free to ask :)


All times are GMT -5. The time now is 03:56 AM.