[英]How do I output the first negative value in a large .csv using powershell?
I need to find the first negative value in a given column in huge (5 million line) .csv
files.我需要在巨大的(500 万行)
.csv
文件中找到给定列中的第一个负值。
Import-Csv .\filename.csv | select "column_header" -First 100 | Select-String '-'
works well enough, but there are 50 of these files and the -First 100
is occasionally more than -First 3000
.工作得很好,但是这些文件中有 50 个,并且
-First 100
偶尔会超过-First 3000
。 This is a job I'll have to do again as we add to it as well.这是一项我必须再做的工作,因为我们也会添加它。 I think the best thing to do would be to pipe this to a function that has some code that does:
我认为最好的办法是 pipe 这个 function 有一些代码:
while(!Found){
if(value.beginsWith("-")) write-output(value); Found=true; break;
}
but I don't know how to stop the input to the pipe (if this is even a feasible way to do it.).但我不知道如何停止对 pipe 的输入(如果这甚至是可行的方法。)。 It is important that we don't read through the entire file (but 5-10k lines would possibly work) - that takes FOREVER.
重要的是我们不要通读整个文件(但 5-10k 行可能会起作用)——这需要永远。
I believe this should get you the first negative result in "Column_Header" and stop after that.我相信这应该让您在“Column_Header”中获得第一个否定结果,然后停止。 Not 100% this is the most efficient way tho, but it should work fine.
不是 100% 这是最有效的方法,但它应该可以正常工作。
Import-Csv .\filename.csv | ?{[math]::Sign($_."column_header") -eq -1} | select -First 1
Some context for the math.Sign
method: https://docs.microsoft.com/en-us/dotnet/api/system.math.sign?view=net-5.0 math.Sign
方法的一些上下文: https://docs.microsoft.com/en-us/dotnet/api/system.math.sign?view=net-5.0
Returns:回报:
Return value![]() |
Meaning![]() |
---|---|
-1 ![]() |
value is less than zero.![]() |
0 ![]() |
value is equal to zero.![]() |
1 ![]() |
value is greater than zero.![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.