如何从PowerShell中删除空字符串/行?
在许多情况下,您需要从PowerShell字符串数组或文件中删除空行或字符串。在本文中,我们将删除结果或从不为空的行中过滤输出,而不是删除空字符串。这样,我们可以获得没有空行的输出。
考虑下面的示例,我们有一个名为EmptryString.txt的文件,我们需要从内容中删除空行。
文本文件的内容如下。
PS C:\Windows\System32> Get-Content D:\Temp\EmptyString.txt This is example of empty string PowerShell PowerShell DSC String Array Hello
您只需要应用行不为空的条件。请参见下面的代码。
示例
Get-Content D:\Temp\EmptyString.txt | where{$_ -ne ""}输出结果
This is example of empty string PowerShell PowerShell DSC String Array Hello
同样,您可以使用上面的命令从字符串数组中删除空行。例如,
$str = "Dog","","Cat","","Camel","","Tiger" $str PS C:\Windows\System32> $str Cat Camel TigerDog
现在应用相同的逻辑来删除空行。
示例
$str | where{$_ -ne ""}输出结果
PS C:\Windows\System32> $str | where{$_ ne ""}
Dog
Cat
Camel
Tiger热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短