用于插入SQL数据库的CSV数据的字符串操作和输出格式
•浏览 1
String Manipulation and output formatting of CSV data for inserting into a SQL Database
我是 PowerShell 新手,我正在尝试将数据从 CSV 日志文件导入 SQL 数据库。
我使用 ForEach 格式化表格并使用 Invoke-Sqlcmd 将值注入到查询中,除了需要更改日期和时间格式外,它工作正常。
我首先重新格式化日期和时间以匹配 smalldatetime 格式。
我浏览了各种网站,试图弄清楚如何将文本格式化回其原始形式。除了将其重新处理回一个新的 CSV 文件并重复该过程之外,我想不出一种方法来完成这个临时任务(而且我什至不知道这是否会按预期工作。)
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
我的输出
$CSVLine 变量显示:
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
之后,格式变为。
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
我不确定如何将输出用于查询或将其重新格式化回之前存在的表。
关于如何进行此操作的任何建议?
提前谢谢你。
- 使用用户提供的标题 Date,ColumnB,ColumnC,ColumnD 导入后
$CSVImport 是一个具有属性的对象 - $CSVLine 也是如此。
你的命令
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
强制 PowerShell 使用对象的属性对对象进行字符串化,从而产生输出
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
您只能在 $CSVLine.Date 上进行替换
但最好按照 thom-schumachers 的回答中的建议转换为 [datetime]。
根据您的语言环境,简单的转换可能不够,然后尝试
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
如果你把你的日期放在 DateTime 类型中
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
然后您将拥有一个日期时间对象,您可以使用它来格式化您的内心内容:
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})', '$3/$1/$2'
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
$CSVLine = $CSVLine -Replace ...
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
[datetime]::ParseExact($CSVLine.Date,"M/d/yyyy HH:mm",$Null)
[datetime]'2017/2/7 13:28'
[datetime]'2017/2/7 13:28' | gm
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
......
有关日期时间的帮助,请参阅过去的这篇文章如何在 PowerShell 中格式化 DateTime