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 |
Your example even is giving syntax errors :
Code:
awk `{print$0 > "Ottawa"substr($1,7,4)".cw2"}` Code:
find ./ -iname *.cw2 -exec awk '{print $0 > "Ottawa"substr($1,7,4)".cw2"}' {} \; Lets assume this: filename.cw2 Code:
1234567890 Code:
awk '{print $0 > "filename"substr($1,7,4)".cw2"}' filename.cw2 Code:
-rw-r--r-- 1 fred fred 11 2009-05-14 16:06 filename7890.cw2 Code:
#!/usr/bin/perl 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 { Could also just do this whole mess in bash using find too. |
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. |
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! |
No problem, if you need additional help please feel free to ask :)
|
All times are GMT -5. The time now is 03:56 AM. |