My script is:
#!/usr/bin/perl -w
my $line="1 2 3 4 5 6 7";
print $line;
my $thirdlast=`print $line |awk '{print $(NF-3)}'`;
print $thirdlast;
The output is:
1 2 3 4 5 6 7 awk: 0602-542 There is an extra ) character.
The source line is 1.
The error context is
{print 201 1 >>> 201NF-3) <<<
Syntax Error The source line is 1.
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
awk: 0602-542 There is an extra ) character.
What is it complaining about? Anything wrong with my script? Couldn't understand why it says The source line is 1
.
What fix is needed by my script?
You don't need to call
awk
inside yourperl
program,perl
provides the necessary functions to perform such operation:This small program outputs:
5
A Sylvain pointed out, you really don't need to call
awk
from withinperl
since the latter can do anything the former can. However, to answer your original question, you need to i) escape the$
inside theawk
, ii) correctly pass your Perl variable to the subshell you launch (print
is something completely different in the shell). Something like:The same thing can be done with
cut
like so: