简体   繁体   English

删除 a.CSV 文件中包含特定字符的行 Powershell

[英]Delete rows in a .CSV file containing specific character with Powershell

I receive an automatic weekly export from a system in a.csv format.我每周从系统收到一个 .csv 格式的自动导出。 It contains a lot of usernames with the initials of the users (eg "fl", "nk").它包含许多带有用户首字母的用户名(例如“fl”、“nk”)。 A few of them have their first and last names, separated by coma (eg firstname.lastname).他们中的一些人有他们的名字和姓氏,用逗号分隔(例如名字.姓氏)。 These are the ones, which have to be deleted from the.csv file.这些是必须从 .csv 文件中删除的。

My goal here is to write a Powershell script, which delete all rows, containing the character "."我的目标是编写一个 Powershell 脚本,它删除所有包含字符“.”的行。 (dot) and then save the same.csv file by overwritting it. (点),然后通过覆盖保存 same.csv 文件。

Since I'm very new to Powershell, I'd highly appreciate a more detailed answer including the potential code.由于我是 Powershell 的新手,我非常感谢您提供更详细的答案,包括潜在代码。 I tried various examples from similar issues, which I found here, but none of them worked and/or I am getting error messages, mostly because my syntax isn't correct.我尝试了来自类似问题的各种示例,我在此处找到了这些示例,但它们都不起作用和/或我收到错误消息,主要是因为我的语法不正确。

Additional info.附加信息。 Here is a part of the table.这是表格的一部分。

在此处输入图像描述

I tried this code:我试过这段代码:

Get-Content "D:\file.csv" | Where-Object {$_ -notmatch '\.'} | Set-Content "D:\File.csv"-Force -NoTypeInformation

As Mathias says, it is helpful to see what you have tried so we can help you come to a working result.正如 Mathias 所说,了解您尝试过的内容很有帮助,这样我们才能帮助您获得有效的结果。 It is easy to give you something like this:很容易给你这样的东西:

$csv = Import-Csv -Path C:\Temp\temp.csv -Delimiter ";"
$newCSV = @()
foreach($row in $csv){
    if(!$row.username -or $row.username -notlike "*.*"){
        $newCSV += $row
    }
}
$newCSV | Export-Csv -Path C:\Temp\temp.csv -Delimiter ";" -NoTypeInformation

The above code eliminates rows that have a dot on the username field.上面的代码消除了在用户名字段上有一个点的行。 It leaves rows with an empty username intact with the 'if(.$row.username' part, But I have no idea whether this is helpful since there is no example CSV file; also there is no way to know what you have tried so far ;)它保留了一个空用户名的行,完整地保留了 'if(.$row.username' 部分,但我不知道这是否有帮助,因为没有示例 CSV 文件;也没有办法知道你尝试了什么远的 ;)

Note that I always prefer using ";"请注意,我总是喜欢使用“;” as delimiter, because opening the file in Excel will already be correctly seperated.作为分隔符,因为在 Excel 中打开文件将已经被正确分隔。 If the current file uses ',' as a delimiter, you will need to change that when importing the CSV.如果当前文件使用 ',' 作为分隔符,您将需要在导入 CSV 时更改它。

You were very close, For this you don't need a loop: you just need to do it using the correct cmdlets:你非常接近,为此你不需要循环:你只需要使用正确的 cmdlet 来完成它:

(Import-Csv -Path 'D:\file.csv' -Delimiter ';') | 
    Where-Object { $_.Initials -notmatch '\.' } | 
    Export-Csv -Path 'D:\file.csv' -Delimiter ';' -Force -NoTypeInformation

Get-Content simply reads a text file and returns the lines as string array, whereas Import-Csv parses the structure and creates objects with properties from the header line. Get-Content只是读取文本文件并将行作为字符串数组返回,而Import-Csv分析结构并创建具有 header 行属性的对象。

The brackets around the Import-Csv are needed to ensure the importing/parsing of the file is completely done before piping the results through.需要Import-Csv周围的括号以确保在通过管道传输结果之前完全完成文件的导入/解析。 Without that, the resulting file may become completely empty because you cannot read and overwrite the same file at the same time.否则,生成的文件可能会完全变空,因为您无法同时读取和覆盖同一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM