Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

awk cmd in perl

by blur (Initiate)
on Oct 11, 2018 at 13:21 UTC ( [id://1223868]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to write a perl-wrapper of sorts to an existing pipeline The pipeline uses a few long awk commands that I just want to be printed into a file (trying to automatically generate the pipeline for new files) But I cant seem to get perl to see the awk script as just words - it freaks at the symbols and after spending a long tome trying to backslash everything that might be a problem I am still getting errors... Any idea how I can get perl to see this line as simple text?

EDIT: Comments about adding script are duely noted

This is the awk command in question:

  awk '{indel=0;for(i=5;i<=NF,i++) {if(i~/REF/){split(i,a,";");split(a[1],b.":");} if(i~/[+-]) {split(a[1],c,":");indel+=c[2]}} print $1"_"$2+1,indel}' > parsed_data

I think that the " and ; symbols are giving perl grief... but I might be wrong

I have other awk cmd that I'd need to use and these are much longer (I don't want to backslash twenty symbols...

Thanks to everyone for their help!

Blur

Replies are listed 'Best First'.
Re: awk cmd in perl (non-interpolated literal strings)
by LanX (Saint) on Oct 11, 2018 at 13:23 UTC
    Due to lack of information I suppose you have an interpolation problem.

    Please try either

    • 'singlequotes'
    • the q{uote-operator} with arbitrary delimiter (pairs)
    • <<'__here_docs__' surrounded by single-quotes.

    see non-interpolating commands listed in perlop#Quote-and-Quote-like-Operators for further information.

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    > get perl to see the awk script as just words

    in case you explicitly need words, see also qw/quoted words/ which returns a list of strings.

      this is awesome! worked like a charm :)
Re: awk cmd in perl
by haukex (Archbishop) on Oct 11, 2018 at 13:59 UTC

    Could you show a Short, Self-Contained, Correct Example, that is, a short piece of example code that shows the problem, with short sample input and the expected output you want to get, and the actual output, including any error messages, each inside <code> tags?

    Going off of your description, I would recommend a module like IPC::System::Simple, its systemx and capturex functions avoid the shell*, and as LanX said, you can provide the commands to be executed in Perl constructs like q{}. Note how in the following, I don't have to escape quotes or $ (it's printing the $$ variable).

    use warnings; use strict; use IPC::System::Simple qw/systemx capturex/; systemx('perl', '-e', q{print "'$$'\n"} ); my $o = capturex('perl', '-e', q{print "'$$'"} ); print "<$o>\n";

    I wrote at length about the issues with running external commands here.

    * Update: Note this also means there won't be any shell redirections or piping available. If you need this, please show and example of the commands you're trying to run, often there is a way to work around it e.g. with capturex, temporary files, or a different module like IPC::Run3 or IPC::Run.

Re: awk cmd in perl
by kcott (Archbishop) on Oct 12, 2018 at 09:14 UTC

    G'day Blur,

    Welcome to the Monastery.

    Here's a pipeline containing awk:

    $ echo "a b c" | awk '{print $2}' b

    Here's a Perl script containing that pipeline. Note that only the '$' needed to be escaped (\$2):

    $ cat pm_1223868_heredoc_awk.pl #!/usr/bin/env perl use strict; use warnings; print <<`EOS`; echo "a b c" | awk '{print \$2}' EOS

    It produces the same output:

    $ pm_1223868_heredoc_awk.pl b

    Look at perlop - Quote-Like Operators - <<EOF and scroll down to the Backticks entry for more about this type of heredoc in Perl.

    [I see others have already mentioned the lack of information and, as this is your first post, I won't go on about it; however, you will need to show us an example with the problem symbols for us to help you further.]

    — Ken

Re: awk cmd in perl
by Laurent_R (Canon) on Oct 11, 2018 at 21:47 UTC
    It may or may not be practical, but perhaps you could consider rewriting the awk scripts into a single Perl program.

    If you don't want to rewrite the awk code manually, you could still try to translate it automatically with the a2p (awk to Perl) utility: see a2p for details.

Re: awk cmd in perl [Solution 2: no escaping]
by kcott (Archbishop) on Oct 12, 2018 at 09:32 UTC

    After posting the heredoc with backticks solution, I thought of another way that required no escaping.

    $ cat pm_1223868_heredoc_awk_2.pl #!/usr/bin/env perl use strict; use warnings; open my $fh, '-|', q[echo "a b c" | awk '{print $2}']; print <$fh>;
    $ pm_1223868_heredoc_awk_2.pl b

    That's just the guts of the solution. You'll need to add error handling at least. See open for more about this.

    — Ken

Re: awk cmd in perl
by Anonymous Monk on Oct 11, 2018 at 16:20 UTC
    awk modules on CPAN (if u can't dl view source and copy with attribution)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1223868]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found