Question

I used

paste Output1.txt Output2.txt | awk '{print $1, $2, $5, $15}' 

inside perl script by using system(awk_commamd). I am getting error like

awk: {print , , , }
awk:        ^ syntax error

Need help to sort this error

Était-ce utile?

La solution 2

It's because Perl is interpolating variables into your string, use single quotes instead:

system('paste Output1.txt Output2.txt | awk \'{print $1, $2, $5, $15}\'');

or:

system(q(paste Output1.txt Output2.txt | awk '{print $1, $2, $5, $15}'));

in any case, it is better to do this all in Perl without using system for increased performance, readability, and portability.

Autres conseils

Duplicating the work of paste and awk using pure perl:

use strict;
use warnings;
use autodie;

my @fhs = map {open my $fh, '<', $_; $fh} qw(Output1.txt Output2.txt);

while (grep {!eof $_} @fhs) {
    my @lines = map {(scalar <$_>) // ''} @fhs;
    chomp @lines;
    my @fields = split ' ', join "\t", @lines;

    print join("\t", grep defined, @fields[0,1,4,14]), "\n";
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top