删除csv中的字符串

时间:2016-02-03 12:48:52

标签: csv powershell replace trim

我尝试在powershell中编写脚本来处理csv。

nagexport.csv

server1;Drive C: Disk Usage
server2;Drive C: Disk Usage
server2;Drive D: Disk Usage
server13.xyz.xxx.asd.qwerty.com;Drive C: Disk Usage
server14.xyz.xxx.asd.qwerty.com;Drive C: Disk Usage
server21.xyz.xxx.asd.qwerty.com;Drive C: Disk Usage
server5;Drive D: Disk Usage
server5;Drive C: Disk Usage
server6;Drive C: Disk Usage
server6;Drive D: Disk Usage
server6;Drive E: Disk Usage
server61.xyz.xxx.asd.qwerty.com;Drive C: Disk Usage
server61.xyz.xxx.asd.qwerty.com;Drive D: Disk Usage
server18;Drive C: Disk Usage
server18;Drive D: Disk Usage

我需要proccess / trim这个文件,输出没有域“xyz.xxx.asd.qwerty.com”。

请求的输出是:

server1;Drive C: Disk Usage
server2;Drive C: Disk Usage
server2;Drive D: Disk Usage
server13;Drive C: Disk Usage
server14;Drive C: Disk Usage
server21;Drive C: Disk Usage
server5;Drive D: Disk Usage
server5;Drive C: Disk Usage
server6;Drive C: Disk Usage
server6;Drive D: Disk Usage
server6;Drive E: Disk Usage
server61;Drive C: Disk Usage
server61;Drive D: Disk Usage
server18;Drive C: Disk Usage
server18;Drive D: Disk Usage

感谢您的帮助

......一杯咖啡和一支香烟...... 怎么样?

$file = "c:\workspace\NagiosExport.csv"
$newfile = "c:\workspace\nagexportnew.csv"

Get-Content $File | ForEach-Object{
    If($_ -match "xyz.xxx.asd.qwerty.com"){
        $_.hostname.Split('.')[0]
    } Else {
        $_
    }
} | Set-Content $newfile

1 个答案:

答案 0 :(得分:1)

gc C:\temp\NagiosExport.csv | % {$_.replace('.xyz.xxx.asd.qwerty.com','')} | Out-File C:\temp\nagexportnew.csv