Reputation: 385
I need some assistance with getting awk to work in perl, or find a better alternative...
This code, returns an error about syntax at | when using the awk command to parse the detail.
my @LIST = `/bin/sh $DIR/ami-show.sh Version: | awk '{print $3}'`;
The exact error is
Use of uninitialized value $3 in concatenation (.) or string at test line 20, <STDIN> line 1.
Any help is greatly appreciated
EDIT: RESOLVED!!
Added \ to the '{print \$3}' all works as expected for this command.
Upvotes: 0
Views: 578
Reputation: 74655
As you have established already, the problem lies with the interpolation of the variable $3
. Instead of escaping the $
to fix the problem, this can be circumvented completely by using perl to split the output of your command.
You should be able to do something like this in perl, using split
rather than relying on awk
. Note that the $3
used in your awk
command corresponds to index number 2, due to arrays starting from index 0.
my $output = `/bin/sh $DIR/ami-show.sh Version:`;
my $column = (split ' ', $output)[2];
split
takes a regular expression as its first argument (the pattern) and splits the string contained in the second argument. The code above splits the output of the command on any amount of white space, which is the same as what awk
does by default.
edit: If you saw my first edit, I simply did split $output
, omitting the pattern argument. This is actually valid, as not specifying a pattern invokes the same, awk-like behaviour.
Upvotes: 3
Reputation: 62105
You want to prevent Perl from interpolating the $3
variable. You could try to separate your awk
command into its own string for better interpolation control:
my $awk = q(awk '{print $3}');
my @LIST = `/bin/sh $DIR/ami-show.sh Version: | $awk`;
Upvotes: 3