我正在使用Powershell处理包含坐标值行的CSV文件。
第1行和第2行是我已经转换为Lat Long值的弧度。
我已经提出了下面的脚本。
我的问题是我不想要每一行 - 它太多了。我想提取每1000行。
foreach ($FileName in get-item $dir\*.* -include test1.csv )
{
$FilenameNP = $Filename | select -expand BaseName
Write-Host $FilenameNP
Write-Output "Latitude,Longitude,Altitude,TimeSeconds,LatitudeRadians,LongitudeRadians,AltitudeMeters,x velocity meters/second ,y velocity meters/second ,z velocity meters/second ,Roll radians ,Pitch radians ,platform heading radians ,wander angle radians ,x body acceleration meters/second2 ,y body acceleration meters/second2 ,z body acceleration meters/second2 ,x body angular rate radians/second ,y body angular rate radians/second ,z body angular rate radians/second " > $dir\Sbet_$FilenameNP.txt
$Content = get-content $Filename
Write-host $Content
foreach ($Line in $Content)
{
$Latitude=$null
$Longitude=$null
$Line=$Line -replace '\s+',''
$LineS=$Line -split ","
$Latitude=([decimal]$LineS[1]*180/[math]::pi)
$Longitude=([decimal]$LineS[2]*180/[math]::pi)
$Altitude=$LineS[3]
Write-Host $Latitude $Longitude $Altitude
write-Output "$Latitude,$Longitude,$Altitude,$Line" >> $dir\Sbet_$FilenameNP.txt
}
}
数据如下所示:
385278.0020318, -0.6458227, 3.0509169, 39.0372952, 0.0044346, 0.0046028,
385278.0070309, -0.6458227, 3.0509169, 39.0373095, 0.0036458, 0.0019423,
385278.0120310, -0.6458230, 3.0509170, 45.1586564, 0.0025192, -0.0011160,
385278.0170301, -0.6458230, 3.0509170, 45.1586851, 0.0013969, -0.0034220,
385278.0220292, -0.6458230, 3.0509170, 45.1587176, 0.0002427, -0.0041081,
385278.0270284, -0.6458230, 3.0509170, 45.1587510, -0.0006602, -0.0027870,
385278.0320285, -0.6458230, 3.0509170, 45.1587844, -0.0012237, -0.0001119,
答案 0 :(得分:1)
$counter = 0
Foreach ($Line in $Content)
{
$counter++
if($counter -ne 10000) {continue}
$counter = 0
# Proceed as normal here
$Latitude=$null
$Longitude=$null
... etc
}
答案 1 :(得分:1)
如果你不想要每一行,不要遍历每一行...使用For
循环并让它迭代你想要提取的行数......
foreach ($FileName in get-item $dir\*.* -include test1.csv )
{
$FilenameNP = $Filename | select -expand BaseName
Write-Host $FilenameNP
Write-Output "Latitude,Longitude,Altitude,TimeSeconds,LatitudeRadians,LongitudeRadians,AltitudeMeters,x velocity meters/second ,y velocity meters/second ,z velocity meters/second ,Roll radians ,Pitch radians ,platform heading radians ,wander angle radians ,x body acceleration meters/second2 ,y body acceleration meters/second2 ,z body acceleration meters/second2 ,x body angular rate radians/second ,y body angular rate radians/second ,z body angular rate radians/second " > $dir\Sbet_$FilenameNP.txt
$Content = get-content $Filename
Write-host $Content
For($i = 0;$i -lt $Content.count;$i=$i+1000){
$Latitude=$null
$Longitude=$null
$Line=$Content[$i] -replace '\s+',''
$LineS=$Line -split ","
$Latitude=([decimal]$LineS[1]*180/[math]::pi)
$Longitude=([decimal]$LineS[2]*180/[math]::pi)
$Altitude=$LineS[3]
Write-Host $Latitude $Longitude $Altitude
write-Output "$Latitude,$Longitude,$Altitude,$Line" >> $dir\Sbet_$FilenameNP.txt
}
}