Regex 在CSV文件中分割字符串
我在CSV文件中有以下数据Regex 在CSV文件中分割字符串,regex,string,powershell,csv,powershell-2.0,Regex,String,Powershell,Csv,Powershell 2.0,我在CSV文件中有以下数据 "Name","SourceIP","DestinationIP" "Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22" "Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22" "Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22" "Sep 1 03:55:57 mmt-5","172.16.48.158",
"Name","SourceIP","DestinationIP"
"Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22"
"Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22"
"Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22"
"Sep 1 03:55:57 mmt-5","172.16.48.158","22.22.22.22"
我需要将Name
列拆分为另外3列<代码>日期,时间
和名称
。像下面这样
"Date","Time","Name","SourceIP","DestinationIP"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
在如何完成上有点迷失。我尝试过用逗号替换第14个字符,但没有成功
$test = import-csv C-sample.csv
$test | foreach-object {
$_.Name = $_.Name.Replace(14, ",")
}
$test | export-csv D-sample.csv -notype
有什么帮助吗?我将使用
regex
解析字符串,并创建一个新的PSCustomObject
将其导出回:
$test = import-csv C-sample.csv
$regex = '(?<Date>\w+\s+\d{1,2})\s+(?<Time>\d{2}:\d{2}:\d{2})\s+(?<Name>.*)'
$test | foreach-object {
$regMatch = [regex]::Match($_.Name, $regex)
[PSCustomObject]@{
Date = $regMatch.Groups['Date'].Value
Time = $regMatch.Groups['Time'].Value
Name = $regMatch.Groups['Name'].Value
SourceIP = $_.SourceIP
DestinationIP = $_.DestinationIP
}
} | export-csv D-sample.csv -notype
编辑:
(?<Date>\w+\s+\d{1,2})\s+(?<Time>\d{2}:\d{2}:\d{2})\s+(?<Name>.*)
"Date","Time","Name","SourceIP","DestinationIP"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
"Sep 1","03:55:57","mmt-5","172.16.48.158","22.22.22.22"
由于您使用的是PowerShell 2,因此无法在哈希表上使用PsCustomObject
强制转换,因此请改用此选项:
$test = import-csv C-sample.csv
$regex = '(?<Date>\w+\s+\d{1,2})\s+(?<Time>\d{2}:\d{2}:\d{2})\s+(?<Name>.*)'
$test | foreach-object {
$regMatch = [regex]::Match($_.Name, $regex)
$object = New-Object PSObject
Add-Member -InputObject $object -memberType NoteProperty -Name Date -Value $regMatch.Groups['Date'].Value
Add-Member -InputObject $object -memberType NoteProperty -Name Time -Value $regMatch.Groups['Time'].Value
Add-Member -InputObject $object -memberType NoteProperty -Name Name -Value $regMatch.Groups['Name'].Value
Add-Member -InputObject $object -memberType NoteProperty -Name SourceIP -Value $_.SourceIP
Add-Member -InputObject $object -memberType NoteProperty -Name DestinationIP -Value $_.DestinationIP
$object
} | export-csv D-sample.csv -notype
$test=导入csv C-sample.csv
$regex='(?\w+\s+\d{1,2})\s+(?\d{2}:\d{2}:\d{2})\s+(?*)
$test |针对每个对象{
$regMatch=[regex]::Match($\名称,$regex)
$object=新对象PSObject
添加成员-InputObject$object-memberType NoteProperty-Name日期-Value$regMatch.Groups['Date'].Value
添加成员-InputObject$object-memberType NoteProperty-Name Time-Value$regMatch.Groups['Time'].Value
添加成员-InputObject$object-memberType NoteProperty-Name-Name-Value$regMatch.Groups['Name'].Value
添加成员-InputObject$object-memberType NoteProperty-Name SourceIP-Value$\源IP
添加成员-InputObject$object-memberType NoteProperty-Name DestinationIP-Value$\ux.DestinationIP
$object
}|导出csv D-sample.csv-notype
谢谢,但我没有得到输出。这就是我得到的<代码>“IsReadOnly”、“IsFixedSize”、“IsSynchronized”、“Keys”、“Values”、“SyncRoot”、“Count”、“False”、“False”、“System.Collections.Hashtable+KeyCollection”、“System.Collections.Hashtable+ValueCollection”、“System.Object”、“5”您在我的脚本中更改了什么吗?如果没有,您使用的是哪个PowerShell版本?那么您可能需要更改pscustomobject的创建方式。让我编辑一下我的答案,效果很好。谢谢你的帮助,不客气。考虑更新你的PopeS壳版本: