Reputation: 33
I am using Powershell to process a CSV file containing lines of coordinate values.
Rows 1 and 2 are radians which I have converted to Lat Long values.
I have come up with the script below which works.
My problem is that I don't want every line - it's too many. I want to extract every 1000th line.
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
}
}
Data looks like this:
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,
Upvotes: 3
Views: 4450
Reputation: 36297
Don't loop through every line if you don't want every line... Use a For
loop and have it iterate at what ever line count you want to extract...
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
}
}
Upvotes: 2
Reputation: 8098
$counter = 0
Foreach ($Line in $Content)
{
$counter++
if($counter -ne 10000) {continue}
$counter = 0
# Proceed as normal here
$Latitude=$null
$Longitude=$null
... etc
}
Upvotes: 1