删除 powershell 输出中的空行...通常
Remove blank lines in powershell output... Generally
我是 PS 脚本的新手。
我发现,一般来说,PS 在其命令的输出中添加了很多换行符。我在下面给出了几个例子,说明我发现 PS 命令中的(默认?)输出有些笼统。
我习惯了类似 Unix 的输出,其中通用输出很少到 none 这些行(当然,没有什么能阻止程序员添加它们)。
是否有一种可配置的方法来减少这些换行符的数量,或许还能减少冗长?
我在下面编制了一份处理特定案例的 SO 问题(和额外的)列表,但 none 旨在提供一般设置。
我可以逐条应用一些答案,但我想问的是是否有某种形式的总体 setting/configuring PS.
编辑:
后来我在 SU.
中发现 this similar question(略有变化)
例子
这是我通过几个命令看到的结果
> dir
****** EXTRA LINE
****** EXTRA LINE
Directorio: C:\Users\USER1\OneDrive - YPF\Documents\soft-hard-ware\windows\powershell ****** VERBOSITY LINE
****** EXTRA LINE
****** EXTRA LINE
Mode LastWriteTime Length Name ****** VERBOSITY LINE
---- ------------- ------ ---- ****** VERBOSITY LINE
-a---- 9/7/2020 17:11 1021 exec_powershell_info.txt
-a---- 9/4/2016 08:25 281 first_bytes.ps1
-a---- 10/7/2020 07:53 1536 get_current_script_name.ps1
-a---- 9/7/2020 11:58 29 hello_world.ps1
-a---- 9/4/2016 09:02 344 last_bytes.ps1
-a---- 14/4/2016 13:08 975 split.ps1
-a---- 10/7/2020 07:54 3108 update-help.txt
****** EXTRA LINE
****** EXTRA LINE
> Get-Culture
****** EXTRA LINE
LCID Name DisplayName ****** VERBOSITY LINE
---- ---- ----------- ****** VERBOSITY LINE
11274 es-AR Español (Argentina)
****** EXTRA LINE
****** EXTRA LINE
向右滚动,可以看到我的评论。
我已将要删除的行标记为“EXTRA LINE
”。
我已经标记了“VERBOSITY LINE
”我想删除的行,但我有点“容忍”更多。
作为比较点,这是我在 Linux 中看到的,我觉得它“不那么冗长”。
$ ll
total 12K
drwxrwxr-x 2 santiago santiago 4,0K ago 10 2017 ./
drwxrwxr-x 17 santiago santiago 4,0K may 19 10:28 ../
-rwxrwxr-x 1 santiago santiago 557 ago 10 2017 date2str.rb*
未解决该问题的其他来源列表
- Remove blank lines Powershell output
- How to remove extra lines from Get-WMIObject output powershell
- Remove Empty Lines From Powershell Output
- Removing extra blank space from PowerShell output
- https://social.technet.microsoft.com/Forums/en-US/229727f6-fdb6-4d95-b585-bd9af3ee6670/removing-blank-spaces-and-blank-lines-from-output?forum=ITCG
您可以定义一个 自定义 Out-Default
函数 从输出中删除所有 empty/blank 行(仅供显示):
function Out-Default {
$Input | Out-String -Stream | Where { $_.Trim().Length -gt 0 } | Write-Host
}
您可以将它放在您的 $PROFILE
文件中,以便在以后的所有会话中都可用(使用 -NoProfile
调用 PowerShell 的会话除外)。
所有命令隐式使用此函数到主机(控制台)输出,因为 Out-Default
是一个特殊命令,PowerShell 在幕后 调用 来处理到主机的输出(即,对于既未捕获也未通过管道传输或重定向的输出)。
因此,一旦函数被定义,简单地调用 Get-ChildItem
(它是 dir
的别名),例如,应该产生移除所有 empty/blank 的输出。
请注意,捕获/管道/重定向输出不会受到影响,这是可取的。
警告:如前所述,Out-Default
仅影响到-host 输出,不影响其他基于格式化系统的 Out-*
cmdlets 的行为,如果需要,您必须单独重新定义。
值得注意的是,重新定义的 Out-Default
不会 影响 Out-File
及其有效别名 >
/ >>
- 请参阅 底部部分 以了解自定义 Out-File
实现,该实现执行相同的空行剥离。
顺便说一句:
PowerShell 带有默认的 Out-Default
命令,作为 cmdlet 实现。
虽然您 可以 直接调用 Out-Default
(无论是默认实现还是自定义实现),但没有理由这样做,并且应该避免这样做 - 请参阅 .
正如 Compo 指出的那样,您可以 使用相同的技术 ad hoc,用于给定的命令;例如:
Get-ChildItem | Out-String -Stream | Where { $_.Trim().Length -gt 0 }
但是请注意,与 Out-Default
方法不同 - 此技术 适合 仅用于显示 ,因为您然后输出 字符串表示 而不是原始对象(如果您要通过管道传输到 Write-Host
,您将完全绕过(成功)输出流)。
自定义 Out-File
函数,它也去除 empty/blank 行并且也被 >
/ >>
调用:
注:
此功能比自定义 Out-Default
功能更复杂,因为它需要支持与原始 cmdlet 相同的参数。
它会比原来的 Out-File
/ >
/ >>
明显 慢 ,而且它不会'不支持常见的 -Confirm
和 -WhatIf
参数(但很少与 Out-File
一起使用)。
如下所示,该函数专为在 PowerShell [Core] 中使用而设计 v6+ - 请参阅源代码中的注释以了解如何(轻松)使其适应 Windows PowerShell.
# Note: Supports neither -Confirm nor -WhatIf
function Out-File {
[CmdletBinding(DefaultParameterSetName = 'ByPath', HelpUri = 'https://go.microsoft.com/fwlink/?LinkID=2096621')]
param(
[Parameter(ParameterSetName = 'ByPath', Mandatory, Position = 0)]
[Alias('Path')]
[string] $FilePath,
[Parameter(ParameterSetName = 'ByLiteralPath', Mandatory, ValueFromPipelineByPropertyName)]
[Alias('PSPath', 'LP')]
[string]
$LiteralPath,
[Parameter(Position = 1)]
# Note: This validation set is for PowerShell *Core* (v6+).
# For Windows PowerShell support, comment out the the next line and uncomment the one after.
[ValidateSet('ascii','bigendianutf32','unicode','utf8','utf8NoBOM','bigendianunicode','oem','utf7','utf8BOM','utf32')]
# [ValidateSet('default', 'ascii','bigendianutf32','unicode','utf8','bigendianunicode','oem','utf7','utf32')]
[string] $Encoding,
[switch] $Append,
[switch] $Force,
[Alias('NoOverwrite')]
[switch] $NoClobber,
[ValidateRange(2, 2147483647)]
[int] $Width,
[switch] $NoNewline,
[Parameter(ValueFromPipeline = $true)]
[psobject] $InputObject
)
$null = $PSBoundParameters.Remove('InputObject')
$Input | Out-String -Stream | Where-Object { $_.Trim().Length -gt 0 } |
Microsoft.PowerShell.Utility\Out-File @PSBoundParameters
}
我是 PS 脚本的新手。 我发现,一般来说,PS 在其命令的输出中添加了很多换行符。我在下面给出了几个例子,说明我发现 PS 命令中的(默认?)输出有些笼统。 我习惯了类似 Unix 的输出,其中通用输出很少到 none 这些行(当然,没有什么能阻止程序员添加它们)。
是否有一种可配置的方法来减少这些换行符的数量,或许还能减少冗长?
我在下面编制了一份处理特定案例的 SO 问题(和额外的)列表,但 none 旨在提供一般设置。 我可以逐条应用一些答案,但我想问的是是否有某种形式的总体 setting/configuring PS.
编辑: 后来我在 SU.
中发现 this similar question(略有变化)例子
这是我通过几个命令看到的结果
> dir
****** EXTRA LINE
****** EXTRA LINE
Directorio: C:\Users\USER1\OneDrive - YPF\Documents\soft-hard-ware\windows\powershell ****** VERBOSITY LINE
****** EXTRA LINE
****** EXTRA LINE
Mode LastWriteTime Length Name ****** VERBOSITY LINE
---- ------------- ------ ---- ****** VERBOSITY LINE
-a---- 9/7/2020 17:11 1021 exec_powershell_info.txt
-a---- 9/4/2016 08:25 281 first_bytes.ps1
-a---- 10/7/2020 07:53 1536 get_current_script_name.ps1
-a---- 9/7/2020 11:58 29 hello_world.ps1
-a---- 9/4/2016 09:02 344 last_bytes.ps1
-a---- 14/4/2016 13:08 975 split.ps1
-a---- 10/7/2020 07:54 3108 update-help.txt
****** EXTRA LINE
****** EXTRA LINE
> Get-Culture
****** EXTRA LINE
LCID Name DisplayName ****** VERBOSITY LINE
---- ---- ----------- ****** VERBOSITY LINE
11274 es-AR Español (Argentina)
****** EXTRA LINE
****** EXTRA LINE
向右滚动,可以看到我的评论。
我已将要删除的行标记为“EXTRA LINE
”。
我已经标记了“VERBOSITY LINE
”我想删除的行,但我有点“容忍”更多。
作为比较点,这是我在 Linux 中看到的,我觉得它“不那么冗长”。
$ ll
total 12K
drwxrwxr-x 2 santiago santiago 4,0K ago 10 2017 ./
drwxrwxr-x 17 santiago santiago 4,0K may 19 10:28 ../
-rwxrwxr-x 1 santiago santiago 557 ago 10 2017 date2str.rb*
未解决该问题的其他来源列表
- Remove blank lines Powershell output
- How to remove extra lines from Get-WMIObject output powershell
- Remove Empty Lines From Powershell Output
- Removing extra blank space from PowerShell output
- https://social.technet.microsoft.com/Forums/en-US/229727f6-fdb6-4d95-b585-bd9af3ee6670/removing-blank-spaces-and-blank-lines-from-output?forum=ITCG
您可以定义一个 自定义 Out-Default
函数 从输出中删除所有 empty/blank 行(仅供显示):
function Out-Default {
$Input | Out-String -Stream | Where { $_.Trim().Length -gt 0 } | Write-Host
}
您可以将它放在您的 $PROFILE
文件中,以便在以后的所有会话中都可用(使用 -NoProfile
调用 PowerShell 的会话除外)。
所有命令隐式使用此函数到主机(控制台)输出,因为 Out-Default
是一个特殊命令,PowerShell 在幕后 调用 来处理到主机的输出(即,对于既未捕获也未通过管道传输或重定向的输出)。
因此,一旦函数被定义,简单地调用 Get-ChildItem
(它是 dir
的别名),例如,应该产生移除所有 empty/blank 的输出。
请注意,捕获/管道/重定向输出不会受到影响,这是可取的。
警告:如前所述,Out-Default
仅影响到-host 输出,不影响其他基于格式化系统的 Out-*
cmdlets 的行为,如果需要,您必须单独重新定义。
值得注意的是,重新定义的 Out-Default
不会 影响 Out-File
及其有效别名 >
/ >>
- 请参阅 底部部分 以了解自定义 Out-File
实现,该实现执行相同的空行剥离。
顺便说一句:
PowerShell 带有默认的 Out-Default
命令,作为 cmdlet 实现。
虽然您 可以 直接调用 Out-Default
(无论是默认实现还是自定义实现),但没有理由这样做,并且应该避免这样做 - 请参阅
正如 Compo 指出的那样,您可以 使用相同的技术 ad hoc,用于给定的命令;例如:
Get-ChildItem | Out-String -Stream | Where { $_.Trim().Length -gt 0 }
但是请注意,与 Out-Default
方法不同 - 此技术 适合 仅用于显示 ,因为您然后输出 字符串表示 而不是原始对象(如果您要通过管道传输到 Write-Host
,您将完全绕过(成功)输出流)。
自定义 Out-File
函数,它也去除 empty/blank 行并且也被 >
/ >>
调用:
注:
此功能比自定义
Out-Default
功能更复杂,因为它需要支持与原始 cmdlet 相同的参数。它会比原来的
Out-File
/>
/>>
明显 慢 ,而且它不会'不支持常见的-Confirm
和-WhatIf
参数(但很少与Out-File
一起使用)。如下所示,该函数专为在 PowerShell [Core] 中使用而设计 v6+ - 请参阅源代码中的注释以了解如何(轻松)使其适应 Windows PowerShell.
# Note: Supports neither -Confirm nor -WhatIf
function Out-File {
[CmdletBinding(DefaultParameterSetName = 'ByPath', HelpUri = 'https://go.microsoft.com/fwlink/?LinkID=2096621')]
param(
[Parameter(ParameterSetName = 'ByPath', Mandatory, Position = 0)]
[Alias('Path')]
[string] $FilePath,
[Parameter(ParameterSetName = 'ByLiteralPath', Mandatory, ValueFromPipelineByPropertyName)]
[Alias('PSPath', 'LP')]
[string]
$LiteralPath,
[Parameter(Position = 1)]
# Note: This validation set is for PowerShell *Core* (v6+).
# For Windows PowerShell support, comment out the the next line and uncomment the one after.
[ValidateSet('ascii','bigendianutf32','unicode','utf8','utf8NoBOM','bigendianunicode','oem','utf7','utf8BOM','utf32')]
# [ValidateSet('default', 'ascii','bigendianutf32','unicode','utf8','bigendianunicode','oem','utf7','utf32')]
[string] $Encoding,
[switch] $Append,
[switch] $Force,
[Alias('NoOverwrite')]
[switch] $NoClobber,
[ValidateRange(2, 2147483647)]
[int] $Width,
[switch] $NoNewline,
[Parameter(ValueFromPipeline = $true)]
[psobject] $InputObject
)
$null = $PSBoundParameters.Remove('InputObject')
$Input | Out-String -Stream | Where-Object { $_.Trim().Length -gt 0 } |
Microsoft.PowerShell.Utility\Out-File @PSBoundParameters
}