Reputation: 1
For the following piece of code, I'm getting the output one below the other (vertically); however what I want is all in a single line (horizontally).
use strict;
use warnings;
use Getopt::Std;
use vars qw($opt_a $opt_d $opt_m $opt_n $opt_o $opt_s $opt_t $opt_l);
my $drives = `wmic volume get name`;
$drives =~ s/Name //;
print $drives;
It should be:
C:\ D:\ F:\ Q:\ X:\
However, what I'm getting is:
C:\
D:\
F:\
Q:\
X:\
Upvotes: 0
Views: 289
Reputation: 126742
For this purpose it's most simple to use the backticks operator in list context; i.e. assign the output to an array. That way you will get one line of output per array element
You also probably want to remove network shares, so a grep
for lines starting with a letter and a colon will extract those
Finally, if you look at the output of wmic
using Data::Dump
or similar, you will see that the lines are padded with spaces so that they are all as long as the longest line. You can use these unwanted spaces using a subtitution, which will also remove the trailing CR and LF
Like this
use strict;
use warnings;
my @drives = grep /^[A-Z]:/, `wmic volume get name`;
s/\s+\z// for @drives;
print "@drives\n";
This is the output of the above code on my own Windows system
X:\ E:\ D:\ L:\ R:\ C:\ P:\ H:\ I:\ S:\ T:\ U:\ J:\ M:\ N:\ O:\ F:\ G:\ Q:\
Upvotes: 0
Reputation: 12493
Replace new line characters with space by using tr///
:
$drives =~ s/Name //;
$drives =~ tr/\r\n/ /;
print $drives;
or by using s///
:
$drives =~ s/Name //;
$drives =~ s/[\r\n]/ /g;
print $drives;
Note: \r
and \n
are new line characters.
Upvotes: 1