Hi, I have a txt file formatted like a csv, I have to check and modify some values based on conditions. I made this code that works, but since the starting file contains 664765 rows it is taking forever to execute, so I was wondering if there was a faster way to do it.
The first line contains the headers (stop_id, arrival_time,departure_time, index)
The arrival and departure time in eache line are always identical and formatted like so hh:mm:ss
Here is what I have to fix, the file contains multiple continuous lines with the same stop _id, it is possible the arrival_time in 2 consecutive lines (with the same stop_id) is the same and that is what I want to change.
For example:
line2 = 1234,08:10:00,08:10:00,1
line3 = 1234,08:10:00,08:10:00,2
In this case I want to change line3 like this:
line3 = 1234,08:10:02,08:10:02,2
Since I wasn’t quite able to find how to modify the lines on my file I created another one and I’m inserting there the lines I check and eventually modify. I don’t know if it takes more time and knowing how to modify the starting file would be faster.
Also I inserted a Write-Host $lineNumberX to check the progress and that could delay everything a bit, but that was the only way I tought for keeping track.
Thanks in advance for the help
$Path1 = "C:\Users\Administrator\Desktop\Mia cartella 5" #Folder with origin file and modified file
$Path2 = ($Path1 + '\' + "stop_times.txt") #origin file
$PathNew = $Path1 + "\OUT.txt" #modified file
$lineNumberX = 2
$line1 = (Get-Content -Path $Path2 -TotalCount 1)
New-Item $PathNew #create modified file
Add-Content -Path $PathNew -Value $line1
Get-Content -Path $Path2 | ForEach-Object {
$lineX = (Get-Content -Path $Path2 -TotalCount $lineNumberX)[-1] #the line I'm checking
$trip_idX = $lineX.Substring(0,11)
$arrival_timeX =$lineX.Substring(12,8)
$lineNumberY = $lineNumberX +1
$lineY = (Get-Content -Path $Path2 -TotalCount $lineNumberY)[-1] #the line after
$trip_idY = $lineY.Substring(0,11)
$arrival_timeY =$lineY.Substring(12,8)
if ($trip_idX -eq $trip_idY)
{
if ($arrival_timeX -eq $arrival_timeY)
{
$lineY = $lineY -replace ":00," , ":02,"
Add-Content -Path $PathNew -Value $lineX
Add-Content -Path $PathNew -Value $lineY
$lineNumberX = $lineNumberX +2
Write-Host $lineNumberX #check video to see how far I am
}
Else
{
Add-Content -Path $PathNew -Value $lineX
$lineNumberX = $lineNumberX +1
Write-Host $lineNumberX #check video to see how far I am
}
}
Else
{
Add-Content -Path $PathNew -Value $lineX
$lineNumberX = $lineNumberX +1
Write-Host $lineNumberX #check video to see how far I am
}
}