从CSV中删除单行 [英] Removing a single line from CSV
问题描述
我有一个包含多行和两列的CSV:名称和信息。
I have a CSV with several rows and two columns: "Name" and "Information".
我正在遍历CSV并检查每行条件,如果条件满足我想删除行:
I'm looping through the CSV and checking each line for a condition, if the condition is met I'd like to remove the line:
for ($i=0; $i -le $CSV.length; $i++)
{
if ($CSV.name == "Fred")
{
#remove $CSV[$i] -- that one line; both "Name" and "Information"
}
}
看到使用 Get-Content
或 Import-Csv
和临时文件但我没有包装我的头围绕它和图必须有一个更容易的方法。
I've seen solutions that use Get-Content
or Import-Csv
and temporary file(s) but I haven't wrapped my head around it and figure there must be an easier way.
无论如何,任何帮助是值得赞赏的!
Regardless, any help is appreciated!
推荐答案
使用 Import-CSV
cmdlet的CSV,并使用 Where-Object
cmdlet过滤条目。最后,使用 Export-CSV
cmdlet回写CSV:
I would read the CSV using the Import-CSV
cmdlet and use the Where-Object
cmdlet to filter the entries. Finally, write the CSV back using the Export-CSV
cmdlet:
$csv = import-csv 'YourPath.csv'
$csv | Where Name -ne 'Fred' | Export-Csv 'YourPath.csv' -NoTypeInformation
这篇关于从CSV中删除单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!