3

我有一个这样配置的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

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

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

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

谢谢你的帮助。

4

3 回答 3

5

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

Import-Csv test.csv | Foreach-Object { $_.Note = [datetime]$_.Note.Split()[-1]; $_}
于 2012-06-09T11:13:43.260 回答
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: "可能会稍微好一些。

于 2012-06-09T03:24:00.060 回答
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 关于在日期后消除杂散字符的问题进行了更新。

于 2012-06-09T04:34:52.220 回答