sas429s has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to pipe my print output to the awk but some how this doesn't seem to be right. Can some one help me to pipe my print command to awk and then redirect that to the file part_no_update.err??

print "$dir $t_spec $part_no $chg_lvl $int_lock error" | \ system("awk '{printf"%-13s %10s %7s %5s %4s - %5s\n",$1,$2,$3,$4,$5,$6 +}' >> /$view_tag/app_hwt/heavy_duty/part_no_update.err"); print "$dir $t_spec $part_no $chg_lvl $int_lock" | \ system("awk '{printf "%-13s %10s %7s %5s %4s - error, can not update p +art number...\n\n",$1,$2,$3,$4,$5}'");
Does the system command work on awk? If so how do i use it? Please advice I am a complete newbie.

Thanks

Replies are listed 'Best First'.
Re: How to use print and pipe it to awk ?
by apl (Monsignor) on Jan 24, 2008 at 14:39 UTC
    Why spend the time piping the test to awk? Perl can do anything awk can; why not have your Perl script do the manipulation?
Re: How to use print and pipe it to awk ?
by johngg (Canon) on Jan 24, 2008 at 16:24 UTC
    As others have mentioned, but not elaborated on, you can achieve all of this in Perl without resorting to Awk. Something like this (not tested)

    #!/usr/bin/perl use strict; use warnings; my $dir = q{/path/to/dir}; my $t_spec = q{xyz}; my $part_no = q{123AZ789}; my $chg_lvl = q{3.2}; my $int_lock = q{LLL}; my $errFile = qq{/$view_tag/app_hwt/heavy_duty/part_no_update.err}; open my $errFH, q{>>}, $errFile or die qq{append: $errFile: $!\n}; my $errFormat = qq{%-13s %10s %7s %5s %4s - %5s\n}; printf $errFH $errFormat, $dir, $t_spec, $part_no, $chg_lvl, $int_lock, q{error}; close $errFH or die qq{close: $errFile: $!\n};

    Hopefully, this will get you started on the task of doing the whole thing in Perl.

    Cheers,

    JohnGG

      Thank you all for the reply. I will try this one out and let you know. Thanks Again
Re: How to use print and pipe it to awk ?
by toolic (Bishop) on Jan 24, 2008 at 14:40 UTC
    Welcome to the Monastery.

    Please use "code" tags around your code (see Writeup Formatting Tips) as this will make it easier for you to communicate your problem.

    Yes, you should be able to use the system command with awk, but you might be better off if you just use Perl's builtin printf function instead. And, instead of using system to append to a file, you could use Perl's open function to append to a file.

      However he wouldn't be able to use system to open an external process to which he could pipe output since it doesn't work that way; for that he'd need to instead open a pipe to it.

      (But that's still not really a great idea in this particular case as everyone's been saying . . .)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re: How to use print and pipe it to awk ?
by Fletch (Bishop) on Jan 24, 2008 at 14:42 UTC
Re: How to use print and pipe it to awk ?
by cdarke (Prior) on Jan 24, 2008 at 20:31 UTC
    For completeness, and those interested in software archeology, you can use system and pipes in awk, here are some examples:
    #Pipe output from print or printf print | "sort" #Pipe input through getline "ls -l" | getline var #Use the system() function call system( "mkdir /home/fred" )
    But you wouldn't use this any more than you would drive to the shops in a chariot.
Re: How to use print and pipe it to awk ?
by roboticus (Chancellor) on Jan 25, 2008 at 04:43 UTC
    You're just piping it to awk which uses printf to format the data, and redirecting it to a file.

    That seems like an awful lot of effort to avoid using perl's printf statement.

    ...roboticus