[英]Vertical output to horizontal output
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:\
Replace new line characters with space by using tr///
: 使用
tr///
将换行符替换为空格:
$drives =~ s/Name //;
$drives =~ tr/\r\n/ /;
print $drives;
or by using s///
: 或使用
s///
:
$drives =~ s/Name //;
$drives =~ s/[\r\n]/ /g;
print $drives;
Note: \\r
and \\n
are new line characters. 注意:
\\r
和\\n
是换行符。
For this purpose it's most simple to use the backticks operator in list context; 为此,在列表上下文中使用反引号运算符是最简单的。 ie 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 您可能还希望删除网络共享,因此,以字母和冒号开头的行的
grep
会提取这些共享
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. 最后,如果使用
Data::Dump
或类似的命令查看wmic
的输出,您将看到这些行用空格填充,因此它们都与最长的行一样长。 You can use these unwanted spaces using a subtitution, which will also remove the trailing CR and LF 您可以通过替换使用这些不需要的空格,这也会删除结尾的CR和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 这是我自己的Windows系统上的上述代码的输出
X:\ E:\ D:\ L:\ R:\ C:\ P:\ H:\ I:\ S:\ T:\ U:\ J:\ M:\ N:\ O:\ F:\ G:\ Q:\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.