2016-01-07 48 views
0

我有以下腳本,由行會行發現在一個CSV文件中的空格PowerShell的突出空白髮現

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Retain blank space." 

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Delete blank space." 

$n = @() 

$f = Get-Content C:\MyPath\*.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} 
     1 {$n+=$item -replace ' '} 
    } 


} else { 
    $n+=$item -replace ' ' 
} 

} 

$n | Set-Content C:\MyPath\*.csv 

的問題是:當有找了個空位,我怎麼能突出在哪裏排隊或在那裏放置顏色,什麼可以減輕發現它的過程?

編輯:不想改變文件或文本,這應該只顯示在PowerShell的控制檯或在ISE的窗口彈出。

+0

這將是您正在使用,以查看該文件的編輯器的功能。不是Powershell。 – EBGreen

+0

用一個特殊的字符替換空格,例如'§',這將很容易找到它然後 –

+0

我得到控制檯中的字符串。不是在一些編輯@EBGreen – CM2K

回答

1

一個基本的代碼示例使用讀主機用戶輸入和改變與寫主背景色在註釋中描述的方法是這樣的:

$str= "test abc1 abc2 test3" 

$index = $str.IndexOf(" ") 
while ($index -gt -1) { 
    write-host $str.Substring(0,$index) -NoNewline 
    write-host "_" -foreground "magenta" -NoNewline 
    $str = $str.Substring($index + 1, $str.length - $index - 1); 
    $index = $str.IndexOf(" ") 
} 
write-host $str.Substring($index + 1, $str.length - $index - 1); 
$confirmation = Read-Host "Do you want to keep the blank on this line?" 
if ($confirmation -eq 'y') { 
    #do action 
} 

編輯:多個白色包含的代碼空間

代碼最初發布:

$n = @() 

$f = Get-Content C:\MyPath\*.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) 

    $str = $item 
    $index = $str.IndexOf(" ") 
    while ($index -gt -1) { 
     write-host $str.Substring(0,$index) -NoNewline 
     write-host "_" -foreground "magenta" -NoNewline 
     $str = $str.Substring($index + 1, $str.length - $index - 1); 
     $index = $str.IndexOf(" ") 
    } 
    write-host $str.Substring($index + 1, $str.length - $index - 1); 
    $confirmation = Read-Host "Do you want to keep the blank on this line?" 
    if (($confirmation -eq 'y') -or ($confirmation -eq 'Y')) { 
     $n+=$item 
    } 
    else { 
     $n+=$item -replace ' ' 
    } 
} else { 
    $n+=$item -replace ' ' 
} 

} 

$n | Set-Content C:\MyPath\*.csv 
+0

謝謝你的答案。剛開始使用PowerShell。我究竟可以如何適應現有的代碼呢?只需將我的代碼粘貼到#do操作部分? – CM2K

+0

@ CM2K我將它添加到您現有的代碼。如果你輸入的東西不是「Y」或「Y」,它會替代空白 – Fabian

+0

很好的答案,謝謝 – CM2K