[英]process multiple CSV file and delete rows in a single column which has double semi colon characters using powershell
consider I have a below CSV file.考虑我有一个下面的 CSV 文件。
input:输入:
ID;ITEM_ID;STATUS;
001;;RELEASED;
002;36530;RELEASED;
003;86246;RELEASED;
004;;RELEASED;
I want to remove the row that has;;我想删除具有的行;; (ITEM_ID) missing and save it.I tried doing it on one sample file and it worked as expected.
(ITEM_ID)丢失并保存它。我尝试在一个示例文件上执行此操作,它按预期工作。
Import-Csv -Path ".\TestFile.CSV" | where {$_.ITEM_ID -ne ""} | Export-Csv -Path ".\TestFile-temp.CSV" -NoTypeInformation
Remove-Item -Path '.\TestDir\TestFile.csv'
Rename-Item -Path '.\TestDir\TestFile-temp.csv' -NewName 'TestFile.csv'
output: output:
ID;ITEM_ID;STATUS;
002;36530;RELEASED;
003;86246;RELEASED;
The challenge is, i have multiple csv files and it doesn't has value in different columns, but in single column when i opened in excel file.挑战是,我有多个 csv 文件,它在不同的列中没有价值,但是当我在 excel 文件中打开时,它在单个列中。 so it's not taking the condition < where {$_.ITEM_ID -ne ""} >.
所以它不采用条件 < where {$_.ITEM_ID -ne ""} >。 Now i have to search/parse each row of each csv file, search special character (;;) in that row and delete the line and save the file.
现在我必须搜索/解析每个 csv 文件的每一行,在该行中搜索特殊字符 (;;) 并删除该行并保存文件。
i am good at shell scripting but, i am very new to powershell scripting.我擅长 shell 脚本,但是我对 powershell 脚本非常陌生。 can anybody please help me to get the logic here or use other cmdlet that can do the job?
谁能帮我在这里获取逻辑或使用其他可以完成这项工作的cmdlet?
$fileDirectory = "C:\Users\Administrator\Documents\check";
foreach($file in Get-ChildItem $fileDirectory)
{
$csvFileToCheck = Import-Csv -Path $fileDirectory\$file
$noDoubleSemiComma = foreach($line in $csvFileToCheck)
{
if(Select-String << i want the logic here>>)
{
$line
}
}
$noDoubleSemiComma | Export-Csv -Path $fileDirectory\tmp.csv -NoTypeInformation
Remove-Item -Path $fileDirectory\$file
Rename-Item -Path $fileDirectory\tmp.csv -NewName $file
}
As commented, you need to add parameter -Delimiter ';'
如评论所述,您需要添加参数
-Delimiter ';'
to the cmdlet otherwise a comma is used to parse the fields in the CSV.到 cmdlet,否则使用逗号解析 CSV 中的字段。
As I understand, you also want to remove the quotes Export-Csv
outputs around all fields and headers and for PowerShell version 7 you have the option to use parameter -UseQuotes AsNeeded
.据我了解,您还想删除所有字段和标题周围的引号
Export-Csv
输出,对于 PowerShell 版本 7,您可以选择使用参数-UseQuotes AsNeeded
。
As this is not available for version 5.1, I made a function ConvertTo-CsvNoQuotes some time ago to remove the quotes in a safe way.由于这不适用于 5.1 版,我前段时间制作了 function ConvertTo-CsvNoQuotes以安全地删除引号。 (simply replacing them all with an empty string is dangerous, because sometimes values do need quotes)
(简单地用空字符串替换它们是危险的,因为有时值确实需要引号)
Copy that function into your script at the top, then below that, your code could be simplified like this:将 function 复制到顶部的脚本中,然后在其下方,您的代码可以简化如下:
$fileDirectory = "C:\Users\Administrator\Documents\check"
Get-ChildItem -Path $fileDirectory -Filter '*.csv' -File | ForEach-Object {
# for better readability store the full path of the file in a variable
$filePath = $_.FullName
(Import-Csv -Path $filePath -Delimiter ';') | ConvertTo-CsvNoQuotes -Delimiter ';' | Set-Content $filePath -Force
Write-Host "File '$filePath' modified"
}
After all helpful suggestion, i finally nailed it down.在所有有用的建议之后,我终于确定了它。 AS my power-shell version was 5.1, i had to use logic for trimming double quotes after export-csv.
因为我的 power-shell 版本是 5.1,所以我不得不在 export-csv 之后使用逻辑来修剪双引号。 Powershell version 7 and later has
-UseQuotes
that could have solve that too. Powershell 版本 7 及更高版本具有
-UseQuotes
也可以解决该问题。 Hope this help others.希望这对其他人有帮助。
$fileDirectory = "C:\Users\Administrator\Documents\check";
foreach($file in Get-ChildItem $fileDirectory)
{
Import-Csv -Path $fileDirectory\$file -Delimiter ';' | where {$_..ITEM_ID -ne ""} | Export-Csv -Path $fileDirectory\temp.csv -Delimiter ';' -NoTypeInformation
$Test = Get-Content $fileDirectory\temp.csv
$Test.Replace('";"',";").TrimStart('"').TrimEnd('"') | Out-File $fileDirectory\temp.csv -Force -Confirm:$false
Remove-Item -Path $fileDirectory\$file
Rename-Item -Path $fileDirectory\temp.csv -NewName $file
Write-Output "$file file modified."
}
Any suggestion to trim down number of lines of code is welcomed.欢迎任何减少代码行数的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.