Powershell:如何删除数组控制台输出中的第一个空行?
Powershell: How to remove first empty blank line in console output of an array?
我有一些代码(tio.run),输出到控制台:
$exeoutput = @(
" Compression : CCITT Group 4",
" Width : 3180",
" Height : 4908"
)
$var = $exeoutput.trim() | Select-String "Height|Width|Compress"
echo ----------------
$var
echo ---------------
输出是
----------------
Compression : CCITT Group 4
Width : 3180
Height : 4908
----------------
如何从控制台输出中删除上 ----------------
之后的第一个空行?
$var
变量的每个元素都是 MatchInfo
类型。您需要将它们转换为 string
,如下所示:
[string[]]$var
或
$var.foreach([string])
阅读ForEach and Where magic methods:
ForEach(type convertToType)
Unique to the ForEach
method, you can pass a type into the ForEach
method if you want to convert every item in a collection into another
type. For example, imagine you have a collection of objects and you
want to convert those objects into their string equivalent. Here is
what that would look like with the ForEach
method:
# Get a collection of processes
$processes = Get-Process
# Convert the objects in that collection into their string equivalent
$processes.foreach([string])
You could have performed the same task by typecasting the collection
into an array of type string (e.g. [string[]]$processes
), and
typecasting the array is in fact significantly faster, however there’s
a very good chance you wouldn’t even notice the difference in
execution time unless you were working with a very, very large
collection. Despite the time difference, I will tend to prefer the
ForEach method syntax in certain situations if it allows me to
maintain elegance in the implementation by avoiding extra round
brackets in the scripts I write.
示例 代码片段(针对 进行了更新):
$var = $exeoutput.trim() | Select-String "Height|Width|Compress"
'-' * 15
$var.foreach([string])
'-' * 15
结果:
---------------
Compression : CCITT Group 4
Width : 3180
Height : 4908
---------------
请注意 echo
是 Write-Output
cmdlet 的别名:
This cmdlet is typically used in scripts to display strings and other
objects on the console. However, because the default behavior is to
display the objects at the end of a pipeline, it is generally not
necessary to use the cmdlet.
请注意 you can multiply numbers, strings, and arrays(请参阅 ˙'-' * 15˙ 而不是 ----------------
)。
我有一些代码(tio.run),输出到控制台:
$exeoutput = @(
" Compression : CCITT Group 4",
" Width : 3180",
" Height : 4908"
)
$var = $exeoutput.trim() | Select-String "Height|Width|Compress"
echo ----------------
$var
echo ---------------
输出是
----------------
Compression : CCITT Group 4
Width : 3180
Height : 4908
----------------
如何从控制台输出中删除上 ----------------
之后的第一个空行?
$var
变量的每个元素都是 MatchInfo
类型。您需要将它们转换为 string
,如下所示:
[string[]]$var
或
$var.foreach([string])
阅读ForEach and Where magic methods:
ForEach(type convertToType)
Unique to the
ForEach
method, you can pass a type into theForEach
method if you want to convert every item in a collection into another type. For example, imagine you have a collection of objects and you want to convert those objects into their string equivalent. Here is what that would look like with theForEach
method:# Get a collection of processes $processes = Get-Process # Convert the objects in that collection into their string equivalent $processes.foreach([string])
You could have performed the same task by typecasting the collection into an array of type string (e.g.
[string[]]$processes
), and typecasting the array is in fact significantly faster, however there’s a very good chance you wouldn’t even notice the difference in execution time unless you were working with a very, very large collection. Despite the time difference, I will tend to prefer the ForEach method syntax in certain situations if it allows me to maintain elegance in the implementation by avoiding extra round brackets in the scripts I write.
示例 代码片段(针对
$var = $exeoutput.trim() | Select-String "Height|Width|Compress"
'-' * 15
$var.foreach([string])
'-' * 15
结果:
---------------
Compression : CCITT Group 4
Width : 3180
Height : 4908
---------------
请注意 echo
是 Write-Output
cmdlet 的别名:
This cmdlet is typically used in scripts to display strings and other objects on the console. However, because the default behavior is to display the objects at the end of a pipeline, it is generally not necessary to use the cmdlet.
请注意 you can multiply numbers, strings, and arrays(请参阅 ˙'-' * 15˙ 而不是 ----------------
)。