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.


In reply to Re: Script to manipulate data is not giving me any output by ELISHEVA
in thread Script to manipulate data is not giving me any output by ebi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.