如何在Powershell中解析csv中列的字符串

时间:2012-06-09 01:20:22

标签: parsing powershell csv substring

我有一个csv配置如下:

PK,INV_AMT,DATE,INV_NAME,NOTE
1,123.44,634,asdfljk,TEST 12OING 06/01/2010 DATE: 04/10/2012
2,123.44,634,wet aaa,HI HOW ARE YOU 11.11 DATE: 01/01/2011
3,123.44,634,dfssdsdfRR,LOOK AT ME NOW….HI7&&& DATE: 06/11/1997
4,123.44,634,asdfsdgg,LOOK AT ME NOW….HI7&&& DATE: 03-21-2097
5,123.44,634,45746345,LOOK AT ME NOW….HI7&&& DATE: 02/18/2000

如何使用powershell在注释列中解析字符串“DATE:”之后的日期?

例如,第一行在注释列中包含字符串“TEST 12OING 06/01/2010 DATE: 04/10/2012”。我需要解析该行中的“04/10/2012”。

我希望能够从上面的csv文件中读取并解析该日期并将其添加为csv文件中的新列。

感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

拆分Note属性的值(默认分隔符为空格),选择最后一个元素(-1)并将其强制转换为日期时间对象。最后,将对象返回到管道($ _)。

Import-Csv test.csv | Foreach-Object { $_.Note = [datetime]$_.Note.Split()[-1]; $_}

答案 1 :(得分:1)

由于DATE: ##########部分位于最后,并且您想将其分成自己的部分,只需将DATE:替换为,即可:

# Open files for reading/writing line by line
$reader = New-Object System.IO.StreamReader("in.csv")
$writer = New-Object System.IO.StreamWriter("out.csv")

# Copy first line over, with an extra ",DATE"
$writer.WriteLine($reader.ReadLine() + ",DATE")

# Process lines until in.csv ends
while (($line = $reader.ReadLine()) -ne $null) {
    # Get index of last occurrence of "DATE: "
    $index = $line.LastIndexOf("DATE: ")

    # Replace last occurrence of "DATE: " with a comma
    $line = $line.Remove($index, 6).Insert($index, ',')

    # Write the modified line to the new file
    $writer.WriteLine($line)
}

# Close the file handles
$reader.Close()
$writer.Close()

如果DATE:之前总是有空格,那么替换" DATE: "代替"DATE: "可能会稍好一些。

答案 2 :(得分:1)

使用正则表达式的替代方法:

Get-Content in.csv |
# Perform a replace on each line with the DATE: pattern. For convenience,
# eliminate preceding whitespace.
Foreach-Object { $_ -replace "\s*DATE: (\d{1,2}[-/]\d{1,2}[-/]\d{2,4}).*",",`$1" } |
Set-Content out.csv

编辑:更新以回应OP关于在日期之后消除流浪字符的问题。