LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-14-2009, 01:28 PM   #1
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Rep: Reputation: 0
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"}`
	    }
}
 
Old 05-14-2009, 03:41 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
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.

Last edited by rweaver; 05-14-2009 at 05:09 PM. Reason: code changes
 
Old 05-14-2009, 07:21 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 05-15-2009, 10:06 AM   #4
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Original Poster
Rep: Reputation: 0
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!
 
Old 05-15-2009, 12:48 PM   #5
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
No problem, if you need additional help please feel free to ask
 
  


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
Shell Script / awk / perl Question cmontr Programming 11 06-25-2008 02:00 PM
awk / perl script help cmontr Programming 3 05-22-2008 05:15 PM
calling awk commands in perl script bharatbsharma Other *NIX 3 11-02-2007 08:07 PM
calling awk commands in perl script bharatbsharma Programming 6 11-02-2007 11:23 AM
awk/perl script for extract data mruknown1 Linux - Newbie 2 09-11-2007 05:42 AM


All times are GMT -5. The time now is 01:02 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration