从字符串中删除字符

时间:2017-12-07 10:48:17

标签: powershell powershell-v2.0 powershell-v3.0

SubClient          Status
POD5_vcprd01
POD4_vcprd01
POD15_VCPRD01_New
POD11_2_vcprd01
POD11_1_vcprd01
POD12_vcprd01
POD13_vcprd01_new
POD10_vcprd01_NEW

我正在尝试删除_new&上表中的_New是CSV格式。我写了下面的剧本:

$report = @()
$Pod_SC = Import-csv "above table"

foreach($Podclient in $pod_sc)
{
$Client = $Podclient.Subclient
$Subclient = $Client.Substring(0,$Client.IndexOf('_N'))
$Status = $Podclient.Status 

$object = New-Object -TypeName PSobject

$object | Add-Member -MemberType NoteProperty -Name "Subclient" -Value $SubClient

$object | Add-Member -MemberType NoteProperty -Name "Status" -Value $Status

$report += $object
}

但它不起作用。预期的产出是

SubClient          Status
POD5_vcprd01
POD4_vcprd01
POD15_VCPRD01
POD11_2_vcprd01
POD11_1_vcprd01
POD12_vcprd01
POD13_vcprd01
POD10_vcprd01

1 个答案:

答案 0 :(得分:0)

只需将其视为文字替换,如果您使用不区分大小写的ireplace,则可以同时处理_New_new

(Get-Content file.csv) -ireplace "_new","" | Set-Content file.csv

您实际上只能使用-ireplace "_new",但如果您想要替换而不是删除语法,则与我的示例-ireplace "_new","_old"

相同