使用用户输入逐行删除空白
remove only blanks line by line with user input
昨天我问了。现在我想做几乎相同的练习,但有一个小改动:如果一行中有一个空白字符(在 CSV 中逐行查看)询问用户是否应删除空白字符;不同之处在于,只有空白而不是整行。
适用于我上一个问题的代码是:
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Retain line."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Delete line."
$n = @()
$f = Get-Content .\test.csv
foreach ($item in $f) {
if($item -like "* *"){
$res = $host.ui.PromptForChoice("Title", "want to keep this line? `n $item", [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no), 0)
switch ($res) {
0 {$n+=$item}
1 {}
}
} else {
$n+=$item
}
}
$n | Set-Content .\test.csv
我觉得应该用Trim()
函数来实现。
所以,我想我应该像下面这样修改 if
子句(对我可能犯的愚蠢的语法错误表示歉意):
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Retain blank."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Delete blank."
$n = @()
$f = Get-Content .\test.csv
foreach ($item in $f) {
if ($item -like "* *") {
$res = $host.ui.PromptForChoice("Title", "want to keep the blank on this line? `n $item", [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no), 0)
switch ($res) {
0 {$n+=$item.Trim()}
1 {}
}
} else {
$n+=$item.Trim()
}
}
$n | Set-Content .\test.csv
这会运行,但仍然会删除该行,所以我是否先修剪它应该无关紧要,我需要修复它以便保留或修剪但不会丢弃。
编辑:
像这样调整 switch ($res)
不起作用:
switch ($res) {
0 {$n+=$item.Trim()}
1 {$n+=$item}
}
} else {
$n+=$item.Trim()
}
Trim()
without parameter removes all whitespace (not just spaces) from beginning and end of a string. You can't use it for removing spaces anywhere else in a string. Instead use the -replace
operator:
$_ -replace ' '
请注意,这次您需要输出未修改的字符串,不仅如果它不包含 space,而且如果用户选择保留现有的 space(s)。
昨天我问了
适用于我上一个问题的代码是:
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Retain line."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Delete line."
$n = @()
$f = Get-Content .\test.csv
foreach ($item in $f) {
if($item -like "* *"){
$res = $host.ui.PromptForChoice("Title", "want to keep this line? `n $item", [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no), 0)
switch ($res) {
0 {$n+=$item}
1 {}
}
} else {
$n+=$item
}
}
$n | Set-Content .\test.csv
我觉得应该用Trim()
函数来实现。
所以,我想我应该像下面这样修改 if
子句(对我可能犯的愚蠢的语法错误表示歉意):
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Retain blank."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Delete blank."
$n = @()
$f = Get-Content .\test.csv
foreach ($item in $f) {
if ($item -like "* *") {
$res = $host.ui.PromptForChoice("Title", "want to keep the blank on this line? `n $item", [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no), 0)
switch ($res) {
0 {$n+=$item.Trim()}
1 {}
}
} else {
$n+=$item.Trim()
}
}
$n | Set-Content .\test.csv
这会运行,但仍然会删除该行,所以我是否先修剪它应该无关紧要,我需要修复它以便保留或修剪但不会丢弃。
编辑:
像这样调整 switch ($res)
不起作用:
switch ($res) {
0 {$n+=$item.Trim()}
1 {$n+=$item}
}
} else {
$n+=$item.Trim()
}
Trim()
without parameter removes all whitespace (not just spaces) from beginning and end of a string. You can't use it for removing spaces anywhere else in a string. Instead use the -replace
operator:
$_ -replace ' '
请注意,这次您需要输出未修改的字符串,不仅如果它不包含 space,而且如果用户选择保留现有的 space(s)。