简体   繁体   English

处理多个 CSV 文件并使用 powershell 删除具有双分号字符的单列中的行

[英]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.

相关问题 如何使用PowerShell计算和使用单个进程的多个实例的内存? - How to calculate and used memory of multiple instance of a single process using powershell? 使用 powershell 替换 csv 文件的列中的值 &lt;0 和 &gt;50 - Replace values <0 and >50 in a column of a csv file using powershell 使用Powershell处理多个csv文件并将数据存储在单独的文本文件中(删除空格和换行) - Process multiple csv files and store data in separate text files (removing spaces and new lines) using Powershell 在Powershell Windows 8中搜索带有双引号的字符串 - Search for string which has double quotes in powershell windows 8 Powershell - 无法解锁 BitLocker,因为 256 个字符的长密码包含带有单双引号的特殊字符 - Powershell - Can't unlock BitLocker as 256 characters long password contains special characters with single double quotes 使用逗号分隔符将单个 CSV 列批处理为多个 - Batch single CSV column to multiple with comma delimiter 使用批处理命令删除由已删除的Win​​dows服务的进程使用的文件 - delete file which is used by a process of deleted windows service using batch command 查找哪个进程正在使用文件 - Find which process is using a file 如何将文本转换为具有分隔符“|”的 csv 文件在 powershell - how to convert text to csv file which having delimiter '|' in powershell NET中的子进程删除文件 - delete a file using a child process in .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM