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

Hello, I want to use awk and sed in a perl script as Im not sure how to do it in a perl script as yet

I want to run this command, awk 'BEGIN {FS="\t"}; {print $1}' | sed 's/.$//' but Im getting the below error

Use of uninitialized value $1 in concatenation (.) or string at ./myperlscript.pl line 50, <stdin> line 1. sed: -e expression #1, char 3: unterminated `s' command

Any pointers or how I can run this in a perl script, or how to do this via perl code, Im still new to perl and programming

Replies are listed 'Best First'.
Re: perl to run awk and sed
by stevieb (Canon) on Nov 30, 2015 at 17:32 UTC

    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

      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

      , 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.
Re: perl to run awk and sed
by runrig (Abbot) on Nov 30, 2015 at 21:08 UTC
    If you want to convert awk or sed to perl, a2p and s2p can do that for you. You can use the results as is, or just get an idea of how to do something in perl. E.g. for your awk, a2p gives:
    $, = ' '; # set output field separator $\ = "\n"; # set output record separator $FS = "\t"; ; while (<>) { chomp; # strip record separator ($Fld1) = split($FS, $_, -1); print $Fld1; }
    s2p outputs more cruft, but the important bit is just 's/.$//', which can be inserted into the above script.
Re: perl to run awk and sed
by toolic (Bishop) on Nov 30, 2015 at 17:31 UTC
    One way might be something like this:
    my $cmd = q(awk 'BEGIN {FS="\t"}; {print $1}' | sed 's/.$//'); system $cmd;
    Single quotes prevent variable interpolation: see perlop

      thanks, sorted now

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

    I've managed to answer it just after posting..., by adding a forward slash's before the variables the command use, so perl would not try and treat them as its variables..I think..., but it works, that said if any can point me to what perl guys would normally use they would use in place of awk/sed for me to do some reaserch on, that would be great

    awk 'BEGIN {FS="\t"}; {print \$1}'| sed 's/.\$//'

      "... that said if any can point me to what perl guys would normally use they would use in place of awk/sed ..."

      That's actually a simple regex capture. Using your example of "myserver.it.com. XXXX XXX XXX XXXXXXXXXXX" (from Re^2: perl to run awk and sed):

      $ perl -wE 'q{myserver.it.com. XXXX XXX XXX XXXXXXXXXXX} =~ /^(.*?)\.\ +s/; say $1' myserver.it.com
      "... for me to do some reaserch"

      Given the simplicity of that regex, further research may be unnecessary; however, on the basis of "Im still new to perl and programming" (from your OP), you might want to start with the very basics in "perlintro: Regular expressions". At the end of that section, you'll find links to documentation with more detailed information.

      — Ken