如何从Powershell中的csv中的列解析字符串 [英] How to parse string from column in csv in Powershell

查看:623
本文介绍了如何从Powershell中的csv中的列解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv配置为这样:

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

如何解析字符串后的日期

例如,第一行有字符串> 。我需要解析该行中的 04/10/2012 '。



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



感谢您的帮助。 p>

解决方案

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

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


I have a csv configured as such:

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

How can I parse the date after the string "DATE:" in the note column using powershell?

For example, the first row has the string "TEST 12OING 06/01/2010 DATE: 04/10/2012" in the note column. I need to parse '04/10/2012' out of that row.

I would like to be able to read from a csv file such as the one above and parse out that date and add it as a new column in the csv file.

Thanks for any help.

解决方案

Split the value of Note property (the default delimiter is space), select the last element (-1) and cast it to a datetime objects. Lastly, return the object back to the pipeline ($_).

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

这篇关于如何从Powershell中的csv中的列解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆