英文:
Delete Specific Rows from a CSV File Using PowerShell
问题
以下是翻译好的部分:
"我组合的脚本确实收集到我需要的结果,但我希望使用更清晰的字符串。在我收集到所有需要的信息后,我将结果导出到一个CSV文件。有三行特定的行我想要移除,所以我重新导入CSV文件并删除这些行。这是我目前正在使用的字符串:
#移除不需要的用户
import-csv .\LDAPCrossWalkTable.csv | Where-Object "User ID" -ne 'test' | Where-Object "User ID" -ne 'process' | Where-Object "User ID" -ne 'app' | export-csv .\LDAPCrossWalkTableFinal.csv -Force -NoTypeInformation
有没有办法使用-或运算符一次使用Where-Object命令,而不是分三次使用管道?我只是无法弄清楚正确的语法。任何意见都会非常感激!谢谢!
更新:感谢Olaf的迅速回应和多个选项!我决定继续使用这个字符串:
import-csv .\LDAPCrossWalkTable.csv | Where-Object {$_. 'User ID' -notin 'test', 'process', 'app'} | export-csv .\LDAPCrossWalkTableFinal.csv -Force -NoTypeInformation"
英文:
The script that I put together does gather the results I need, but I was hoping to utilize a cleaner string.
After I gather all the needed information, I export the results to a csv file. There are three specific rows I would like to remove, so I re-import the csv file and delete those rows. This is the string I am currently utilizing:
#Remove Unneeded Users
import-csv .\LDAPCrossWalkTable.csv | Where-Object "User ID" -ne 'test' | Where-Object "User ID" -ne 'process' | Where-Object "User ID" -ne 'app' | export-csv .\LDAPCrossWalkTableFinal.csv -Force -NoTypeInformation
Is there a way to use the Where-Object cmdlet one time by using the -or operator rather than pipe 3 separate times? I just can't figure out the correct syntax. Any input it greatly appreciated! Thanks!
UPDATE: Thank you, Olaf, for the prompt response and multiple options! I decided to proceed with this string:
import-csv .\LDAPCrossWalkTable.csv | Where-Object {$_.'User ID' -notin 'test', 'process', 'app'} | export-csv .\LDAPCrossWalkTableFinal.csv -Force -NoTypeInformation
答案1
得分: 4
有一种方法可以将多个筛选条件组合到一个 Where-Object
中。你可以在将结果导出到CSV文件之前应用这些筛选条件。
使用 -ne
进行多次筛选:
Where-Object {
$_.'User ID' -ne 'test' -and
$_.'User ID' -ne 'process' -and
$_.'User ID' -ne 'app'
}
如@Santiago Squarzon所指出的评论,另一种方法是使用 -notin
:
Where-Object {
$_.'User ID' -notin 'test', 'process', 'app'
}
或者,也可以这样建议:
Where-Object 'User ID' -notin 'test', 'process', 'app'
@Theo建议的第三种方法是使用正则表达式,如下所示:
Where-Object 'User ID' -notmatch '\b(test|process|app)\b'
或者,如果需要完全匹配,可以使用以下方式:
Where-Object 'User ID' -notmatch '^(test|process|app)$'
现在你有多种选择来进行筛选。 😉
英文:
Yes, there is a way to combine more than one filter in one Where-Object
. And you can apply this filter before you export your results to a CSV file
Where-Object {
$_.'User ID' -ne 'test' -and
$_.'User ID' -ne 'process' -and
$_.'User ID' -ne 'app'
}
As pointed out by the comment of @Santiago Squarzon ... and alternative to having multiple -ne
would be to use -notin
.
Where-Object {
$_.'User ID' -notin 'test', 'process', 'app'
}
... or ... as suggested like this:
Where-Object 'User ID' -notin 'test', 'process', 'app'
A third way recommended by @Theo would be top use regex like this:
Where-Object 'User ID' -notmatch '\b(test|process|app)\b'
or like this if it should be a full match
Where-Object 'User ID' -notmatch '^(test|process|app)$'
Now you have plenty of options to choose from.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论