gpt4 book ai didi

powershell - 使用PowerShell删除行

转载 作者:行者123 更新时间:2023-12-02 23:17:13 26 4
gpt4 key购买 nike

我想在使用Powershell中删除空行。
这是我的文字

Line 1
Line 2
<Empty line>
<Empty line>
Line 3
Line 4

我试过使用-replace(“`n”,“”),但是它不起作用。
请帮忙!

最佳答案

$text = """
Line 1
Line 2


Line 3
Line 4
"""

$text -replace "(?m)^\s*`r`n",''
# Or
$text -replace "(?m)^\s*`n",''

正则表达式解释:
(?m) set the multiline flag so ^ matches the beginning of each line and not just the begining of the whole string.
^ matches the beginning of a line
\s* matches any number of white spaces (could also be 0 white spaces)
`r`n matches the end of a line.

为什么`r`n或只是`n(取决于机器或文件):
\`r = CR (Carriage Return) → Used as a new line character in Mac OS before X
\`n = LF (Line Feed) → Used as a new line character in Unix/Mac OS X
\`r\`n = CR + LF → Used as a new line character in Windows

关于powershell - 使用PowerShell删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62412389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com