Streamlining Powershell based file edition
So I currently have a group of csv files that I comb through and replace various portions of using powershell. However, the current process I use is highly time intensive, and I was wondering if there was a better more streamlined solution.
Current code: (This is all contained in a for each loop, where $file is the file location and $tb is the name of 开发者_运维问答a table)
Invoke-sqlcmd -query "select Distinct * from $($tb.name) WITH (NoLock)" -server server -database database | Export-CSV $file -Delimiter "|" -Encoding "Unicode" -NoTypeInformation
(get-content $file) -replace '"?\|"?', "|" | out-file $file
(get-content $file) -replace '\|true\|', '|1|' | out-file $file
(get-content $file) -replace '\|false\|', '|0|' | out-file $file
(get-content $file) -replace '^"' | out-file $file
(get-content $file) -replace '"$' | out-file $file
(get-content $file) -replace '^true\|', '1|' | out-file $file
(get-content $file) -replace '^false\|', '0|' | out-file $file
(get-content $file) -replace 'true$', '1' | out-file $file
(get-content $file) -replace 'false$', '0' | select -Skip 1 | out-file $file
(get-content $file) -replace '\|false\|', '|0|' | out-file $file
}
Upvoted answer by @EBGreen, that's probably the solution. Just to show you how to do it (and how to chain operators), I'll add the code:
Invoke-SqlCmd...
(get-content $file) -replace '"?\|"?', "|" `
-replace '\|true\|', '|1|'`
...
-replace '\|false\|', '|0|' | out-file $file
Besides that consider replacing earlier before you export the data as csv. You may go through the collection from Invoke-SqlCmd
and replace appropriate property values.
Also consider if simply replacing true
with 1
and false
with 0
would remove most of the regexes.
The first and most obvious thing that I see is that you load the file off the disk and write it back for every replace. The first optimization that I would be is to load the file into memory once, do all the replaces on it in memory, then write it back out to disk.
Something like:
$contents = (get-content $file) -replace '"?\|"?', "|"
$contents = $contents -replace '\|true\|', '|1|'
e
t
c
.
.
.
$contents | out-file $file
精彩评论