Powershell csv selects every nth row
I am using Powershell to process a CSV file containing strings of coordinate values.
Rows 1 and 2 are radians which I converted to Lat Long values.
I came up with a script below which works.
My problem is, I don't want every line - there are too many. I want to extract every thousandth row.
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
}
}
The 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,
+3
source to share
2 answers
Don't iterate over every line unless you want every line ... Use a loop For
and iterate over whatever you want to output from the string ...
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
}
}
+1
source to share