简体   繁体   English

使用PowerShell从csv文件获取计数并导出到csv

[英]Using PowerShell to get a count from a csv file and export to csv

I have a csv file with name,id,age. 我有一个名称,编号,年龄的csv文件。 I have imported it properly and am looking for a count of all names that are null(empty). 我已经正确导入了它,并且正在寻找所有为空(空)的名称的计数。 I have this so far: 到目前为止,我有:

@(Import-Csv C:\Mine\Desktop\f1.txt | Where-Object {$_.name -eq ""}).Count

This prints out the correct number of empty names into the prompt, but if I export it, nothing shows up in the txt file. 这会在提示符中打印出正确数量的空名称,但是如果我将其导出,则txt文件中不会显示任何内容。

@(Import-Csv C:\Mine\Desktop\f1.txt | Where-Object {$_.name -eq ""}).Count | Export-Csv C:\Mine\Desktop\tests.txt -notype

So it shows the correct number in the command prompt, but when I try to export it, it creates the .txt but it is empty. 因此,它在命令提示符下显示了正确的数字,但是当我尝试导出它时,它将创建.txt,但它为空。 Any help would be appreciated. 任何帮助,将不胜感激。 Also, if I wanted to know how many null entries their are in the entire CSV (from name,id,age). 另外,如果我想知道整个CSV中有多少个空条目(根据名称,id,年龄)。 Would the best way be to do this line Where-Object {$_.name -eq ""} but change $_.name every time? 最好的方法是执行此行Where-Object {$_.name -eq ""}但每次更改$_.name Where-Object {$_.name -eq ""}吗? Or is there a shorter version? 还是有一个较短的版本?

The problem is that you are taking an integer (the count of entries with blank names) and trying to export a CSV. 问题是您要获取一个整数(具有空白名称的条目数)并尝试导出CSV。 Export-CSV expects you to have an object, or array of objects with properties that it can convert to a table in CSV format, with property names as the header row, and values below that for each object. Export-CSV期望您具有一个对象或具有属性的对象数组,可以将其转换为CSV格式的表,并以属性名称作为标题行,并且每个对象的值都低于该值。

What you want is to just output that value to a file, so try replacing Export-CSV with Set-Content (and remove the -notype ) to set the content of the file you specify to the number delivered by your command. 您想要的只是将该值输出到文件,因此请尝试使用Set-Content替换Export-CSV (并删除-notype )以将指定文件的内容设置为命令传递的编号。

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

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