in reply to perl to run awk and sed

perl can do all of this internally.

Why don't you show us a few lines of your input you're sending to these commands, your expected output after they're processed, and we'll see if we can avoid you shelling out?

Here's an attempt of what I think you want though:

use warnings; use strict; my @lines = ( # where '\t' is a real tab "one\ttwo\tthree", "four\tfive\tsix", ); for my $line (@lines){ my $str = (split /\t/, $line)[0]; $str =~ s/.$//; print "$str\n"; } __END__ on fou

Replies are listed 'Best First'.
Re^2: perl to run awk and sed
by deelinux (Novice) on Nov 30, 2015 at 17:39 UTC

    I want to run the awk and sed on a file, which has the below

    myserver.it.com. XXXX XXX XXX XXXXXXXXXXX

    So I want to get the server names from a file and pipe this via awk and sed into another file

    The awk is to get the first line (server name) and the sed is to get rid of the last. (full stop).

      My code above pretty well does this without shelling out:

      use warnings; use strict; my @lines = ( "myserver.it.ca. XXXX XXX XXX XXXXXXXXXXX", "myserver.it.org. XXXX XXX XXX XXXXXXXXXXX", "myserver.it.com. XXXX XXX XXX XXXXXXXXXXX", ); for my $line (@lines){ my $server = (split /\s+/, $line)[0]; $server =~ s/\.$//; print "$server\n"; } __END__ myserver.it.ca myserver.it.org myserver.it.com

        Since interest is limited to the very first returned value, we can limit the split and save a little time.

        my $server - (split /\s+/, $line, 2)[0];

        The optional third parameter limits the number of returned values to 2, so perl doesn't waste time splitting the rest of the line.

        Dum Spiro Spero

        many thanks, time to digest and play

Re^2: perl to run awk and sed
by deelinux (Novice) on Nov 30, 2015 at 17:40 UTC

    , thanks :-) , will have a play later, as I d rather use perl where I can

      I d rather use perl where I can
      ++. I would also advise you to do it in pure Perl, which can do anything that awk and sed can do, and much more, and in most cases more efficiently and with much richer functionalities.

        ++ ...it's also much more likely your code will be cross-platform portable (unix, windows etc), if you're not shelling out to commands that are platform specific.