in reply to Script to manipulate data is not giving me any output
I first began using Perl when an old AWK script became unmanagable. a2p (already cited by swampyankee above) converts AWK scripts to Perl and was tremendously helpful for learning how do things in a Perlish way. I strongly recommend experimenting with it if you are familiar with AWK and are learning Perl.
Unfortunately, it just dumps the Perl code and doesn't really explain how constructs in AWK and Perl compare, so I've added an annotated example here. Note the difference in order. On the bash command line, you use the order: (a) specify input (b) specify processing (c) specify output. Here we switch (b) and (c), and open both the input and output streams before processing.
use strict; use warnings; # open an input stream # same effect as: # cat /mnt/scripts/lagtime/tmp/input.txt| # if fail, print reason ($!) and abort open IN, '<', '/mnt/scripts/lagtime/tmp/input.txt' or die "Couldn't open input file: $!"; # open an output stream # same effect as: # > /tmp/snapvault_status.out.out; # if fail, print reason ($!) and abort open OUT, '>', '/tmp/snapvault_status.out.out' or die "Couldn't open output file: $!"; # equivalent to your awk program # { printf "%1.50s\t%0.60s\t%0.45s\t %0.45s\n", $1, $2, $3, $4 } while (my $line=<DATA>) { # chomp: remove record separator # split: break into fields using default AWK field separator # (a single space which is equivalent to the Perl regex m{ } my @aFields = split(m{ }, $line); chomp $line; printf OUT "%1.50s\t%0.60s\t%0.45s\t %0.45s\n", @aFields }
Also, when you open an input or output stream, always check for errors. There are ever so many reasons why a file doesn't get opened when you think it should. If you use open ... or die ..., you will save yourself hours scratching your head wondering why your program doesn't print anything.
To learn more about the commands in this script, see open, chomp, and split. You may also find these in depth tutorials on opening files (perlopentut) and regular expressions (perlretut) helpful.
Best, beth
Update: Added links to relevant Perl documentation.
|
|---|