如果一行包含使用powershell或Javascript的=符号后的空值,如何删除行?
我的输入值是
variable1 = value1
variable2 = value2
variable3 =
variable4 = value4
variable5 =
这些值可能会改变.i am,以寻找一个脚本,如果该值是空的,请在==符号之后删除行。
发布于 2021-11-14 13:13:37
看起来您正在尝试修改.INI文件,这样做实际上可能会破坏一些应用程序的初始化。
话虽如此,在PowerShell中,您可以做到:
$result = Get-Content -Path 'X:\theInputFile.txt' | Where-Object { $_ -match '^\w+\s*=\s*\S+' }
$string = @'
variable1 = value1
variable2 = value2
variable3 =
variable4 = value4
variable5 =
'@
$result = $string -split '\r?\n' | Where-Object { $_ -match '^\w+\s*=\s*\S+' }
变量$result
将是一个字符串数组:
variable1 = value1
variable2 = value2
variable4 = value4
可以使用以下方法将其保存回(新)文件
$result | Set-Content -Path 'X:\theNewInputFile.txt'
Regex详细信息:
^ Assert position at the beginning of the string
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
= Match the character “=” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\S Match a single character that is a “non-whitespace character”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
https://stackoverflow.com/questions/69963055
复制相似问题