逐行替换文件中的空白字符
Replace blank characters from a file line by line
我希望能够从 CSV 文件中找到所有空白,如果在一行中发现空白字符,则应该出现在屏幕上,并且应该询问我是否要保留包含以下内容的整行那个白色 space 或删除它。
假设目录是 C:\Cr\Powershell\test
。里面有一个 CSV 文件 abc.csv
.
尝试这样做,但在 PowerShell ISE 中无法识别 $_.PSObject.Properties
。
$csv = Import-Csv C:\Cr\Powershell\test\*.csv | Foreach-Object {
$_.PSObject.Properties | Foreach-Object {$_.Value = $_.Value.Trim()}
}
我为没有包含更多代码和我到目前为止尝试的更多内容而道歉,但自从我刚开始以来,它们都是愚蠢的尝试。
This 看起来很有帮助,但我不知道如何针对我的问题调整它。
好的伙计,你开始吧:
$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
如有疑问请在评论中post,我会解释
Get-Content
可能是比 Import-Csv
更好的方法,因为这将允许您检查整行是否有空格,而不必检查每个单独的字段。对于全自动处理,您只需使用 Where-Object
过滤器从输出中删除 non-matching 行:
Get-Content 'C:\CrPowershell\test\input.csv' |
Where-Object { $_ -notlike '* *' } |
Set-Content 'C:\CrPowershell\test\output.csv'
但是,由于您要提示包含空格的每一行,您需要一个 ForEach-Object
(或类似的构造)和一个嵌套条件,如下所示:
Get-Content 'C:\CrPowershell\test\input.csv' | ForEach-Object {
if ($_ -notlike '* *') { $_ }
} | Set-Content 'C:\CrPowershell\test\output.csv'
提示用户输入的最简单方法是 Read-Host
:
$answer = Read-Host -Prompt 'Message'
if ($answer -eq 'y') {
# do one thing
} else {
# do another
}
在你的特定情况下,你可能会对任何匹配的行做这样的事情:
$anwser = Read-Host "$_`nKeep the line? [y/n] "
if ($answer -ne 'n') { $_ }
以上检查答案是否不是 n
以做出删除该行的明智决定。
提示用户输入的其他方法是 choice.exe
(它具有允许超时和默认答案的额外优势):
choice.exe /c YN /d N /t 10 /m "$_`nKeep the line"
if ($LastExitCode -ne 2) { $_ }
或 host UI:
$title = $_
$message = 'Keep the line?'
$yes = New-Object Management.Automation.Host.ChoiceDescription '&Yes'
$no = New-Object Management.Automation.Host.ChoiceDescription '&No'
$options = [Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$answer = $Host.UI.PromptForChoice($title, $message, $options, 1)
if ($answer -ne 1) { $_ }
我将它留作练习,供您将您选择的任何提示例程与其余代码集成。
我希望能够从 CSV 文件中找到所有空白,如果在一行中发现空白字符,则应该出现在屏幕上,并且应该询问我是否要保留包含以下内容的整行那个白色 space 或删除它。
假设目录是 C:\Cr\Powershell\test
。里面有一个 CSV 文件 abc.csv
.
尝试这样做,但在 PowerShell ISE 中无法识别 $_.PSObject.Properties
。
$csv = Import-Csv C:\Cr\Powershell\test\*.csv | Foreach-Object {
$_.PSObject.Properties | Foreach-Object {$_.Value = $_.Value.Trim()}
}
我为没有包含更多代码和我到目前为止尝试的更多内容而道歉,但自从我刚开始以来,它们都是愚蠢的尝试。
This 看起来很有帮助,但我不知道如何针对我的问题调整它。
好的伙计,你开始吧:
$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
如有疑问请在评论中post,我会解释
Get-Content
可能是比 Import-Csv
更好的方法,因为这将允许您检查整行是否有空格,而不必检查每个单独的字段。对于全自动处理,您只需使用 Where-Object
过滤器从输出中删除 non-matching 行:
Get-Content 'C:\CrPowershell\test\input.csv' |
Where-Object { $_ -notlike '* *' } |
Set-Content 'C:\CrPowershell\test\output.csv'
但是,由于您要提示包含空格的每一行,您需要一个 ForEach-Object
(或类似的构造)和一个嵌套条件,如下所示:
Get-Content 'C:\CrPowershell\test\input.csv' | ForEach-Object {
if ($_ -notlike '* *') { $_ }
} | Set-Content 'C:\CrPowershell\test\output.csv'
提示用户输入的最简单方法是 Read-Host
:
$answer = Read-Host -Prompt 'Message'
if ($answer -eq 'y') {
# do one thing
} else {
# do another
}
在你的特定情况下,你可能会对任何匹配的行做这样的事情:
$anwser = Read-Host "$_`nKeep the line? [y/n] "
if ($answer -ne 'n') { $_ }
以上检查答案是否不是 n
以做出删除该行的明智决定。
提示用户输入的其他方法是 choice.exe
(它具有允许超时和默认答案的额外优势):
choice.exe /c YN /d N /t 10 /m "$_`nKeep the line"
if ($LastExitCode -ne 2) { $_ }
或 host UI:
$title = $_
$message = 'Keep the line?'
$yes = New-Object Management.Automation.Host.ChoiceDescription '&Yes'
$no = New-Object Management.Automation.Host.ChoiceDescription '&No'
$options = [Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$answer = $Host.UI.PromptForChoice($title, $message, $options, 1)
if ($answer -ne 1) { $_ }
我将它留作练习,供您将您选择的任何提示例程与其余代码集成。