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

Have the following code and i wish that the variable that store the system output to be places in a single variable. However a newline is printer at the end of each variableso i get the following when i print brand model size

#!/usr/bin/perl use strict; use warnings; my $dcr = "na"; my $dev= "/dev/sdc" ; my $brand = qx(/usr/bin/lsscsi | grep $dev |awk '{print \$4}' ) ; my $model = qx(/usr/bin/lsscsi | grep $dev |awk '{print \$5}' ) ; my $disk_size = `/sbin/parted $dev -s print | grep Disk | awk '{pr +int \$3}' `; $dcr = "$brand $model $disk_size"; print "$dcr";

Replies are listed 'Best First'.
Re: System output variables and newline
by roboticus (Chancellor) on Jul 08, 2014 at 01:45 UTC

    Jocqui:

    Rather than executing lsscsi twice, you could execute it once, and then split the line into pieces. That way, you'll save one execution of lsscsi, one execution of grep and two executions of awk. It should look something like this:

    my $tmp = qx( /usr/bin/lsscsi | grep $dev ); my (undef, undef, undef, $brand, $model, undef) = split /\s+/,$tmp;

    The undefs are used to ignore the chunks of the line you don't want.

    You can even save the external execution of grep by using the grep built into perl to make it a little more perly.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: System output variables and newline (remove newlines)
by Anonymous Monk on Jul 07, 2014 at 20:56 UTC
Re: System output variables and newline
by AppleFritter (Vicar) on Jul 07, 2014 at 21:45 UTC