简体   繁体   English

在CSV文件中分割字符串

[英]Divide a string in a CSV file

I have the following data in a CSV file 我在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","22.22.22.22"

I need to split the Name column in to 3 more columns. 我需要将“ Name列分成另外3列。 Date , Time and Name . DateTimeName Like the following 像下面

"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"

A bit lost on how to accomplish. 在完成方面有些失落。 I have tried this to replace the 14th character with a comma but no luck. 我试过用逗号代替第14个字符,但没有运气。

$test = import-csv C-sample.csv

$test | foreach-object {
    $_.Name = $_.Name.Replace(14, ",")
}

$test | export-csv D-sample.csv -notype

Any help? 有什么帮助吗?

I would parse the string using a regex and create a new PSCustomObject to export it back: 我将使用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

Regex: 正则表达式:

(?<Date>\w+\s+\d{1,2})\s+(?<Time>\d{2}:\d{2}:\d{2})\s+(?<Name>.*)

正则表达式可视化


Output: 输出:

"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"

Edit: 编辑:

Since you are using PowerShell 2, you can't use the PsCustomObject cast on a hashtable so use this instead: 由于您使用的是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM