Author Posts

July 15, 2016 at 5:04 pm

Hi,
I have log file which contains the below information:
Data started: 02:39
Data ended: 04:25
Time taken: 60000

Data started: 04:39
Data ended: 02:25
Time taken: 70000

I want "Time Taken" value to convert into minutes.

Can someone please help me on this?

-Kalyan

July 15, 2016 at 5:09 pm

What is the value now? Seconds?

July 15, 2016 at 5:10 pm

New-TimeSpan -Seconds 70000
New-Timespan -Seconds (70000/1000)
[Math]::Round(70000/60)
[Math]::Round(70000/(1000*60))

You can use New-Timespan or just a math equation.

  • This reply was modified 2 years, 1 month ago by  Craig Duff.
  • This reply was modified 2 years, 1 month ago by  Craig Duff.
  • This reply was modified 2 years, 1 month ago by  Craig Duff.

July 15, 2016 at 5:15 pm

Hi Jones,
Now, it is milliseconds.

-Kalyan

July 15, 2016 at 5:16 pm

Hi Craig,
Thanks. But in the log file, every third line "Time taken" filed is there.

How do I convert every "Time Taken" field.

NOTE: The "Time taken" value keeps on changing.

-Kalyan

July 15, 2016 at 5:18 pm

So you are trying to rewrite the log file with that line converted?

July 15, 2016 at 5:20 pm

It's actually "Don" or "Mr. Jones" 🙂

So if the start is...

Data started: 02:39
Data ended: 04:25
Time taken: 60000

Data started: 04:39
Data ended: 02:25
Time taken: 70000
foreach ($line in (Get-Content filename.txt)) {
 if ($line -like '*time*') {
    $pieces = $line -split ' '
    $milliseconds = $pieces[1]
    $seconds = $milliseconds / 1000
 }
}

and then you can do whatever you want with $seconds.

July 15, 2016 at 5:27 pm

Hi Craig,
Or we can output the result to a new file.
Same file will also good.

-Kalyan

July 15, 2016 at 5:31 pm

Hi Don,
Tried the below:
foreach ($line in (Get-Content VIS_PRD_Data_Aggregation_Log.txt)) {
if ($line -like 'Time taken for whole process') {
$pieces = $line -split ' '
$milliseconds = $pieces[1]
$seconds = $milliseconds / 1000
}
}

Nothing is happening.

-Kalyan

July 15, 2016 at 5:45 pm

Hi,
It says:
Cannot convert value "taken" to type "System.Int32". Error: "Input string was not in a correct
format."
At C:\Users\admin\Desktop\Test\cool\test.ps1:5 char:6
+ $seconds = $milliseconds / 1000
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger

-Kalyan

July 15, 2016 at 5:51 pm

Apologies.

foreach ($line in (Get-Content filename.txt)) {
 if ($line -like '*time*') {
    $pieces = $line -split ' '
    $milliseconds = $pieces[2]
    $seconds = $milliseconds / 1000
    # for example... "Time Taken: $seconds" | Out-File whatever.txt -Append
 } else {
  # for example... $line | Out-File whatever.txt -Append
 }
}

However, this isn't intended to be a complete solution for you, as I don't know what your end goal is. My intent was to help you pull out the milliseconds value and convert it to seconds. That value is in $seconds. You will have to do additional coding to achieve your final desired result. Our goal here is to help you, but speaking for myself I'm not able to take the time and analyze your needs and produce a completed script free of charge. Sorry about that miscommunication. I've added two comments to the above that can perhaps show how I would use $seconds to write out to a new log file.

July 15, 2016 at 6:00 pm

Thanks. Let me try on this.

-Kalyan

July 15, 2016 at 7:25 pm

Modified the script & made it working: Below is the script:
foreach ($line in (Get-Content test.txt)) {
if ($line -like '*process*') {
$pieces = $line -split ' '
$seconds = [math]::round( ($pieces[6]) / 1024)
write-host $seconds
}
}

Just have a question, by using the script I am able to convert milliseconds to minutes.

Can I replace these values in the log:
Data started: 02:39
Data ended: 04:25
Time taken: 60000

Data started: 04:39
Data ended: 02:25
Time taken: 70000

-Kalyan

July 16, 2016 at 1:24 am

Hi,
Finally modified the script a bit & able to convert to minutes and replace in the same file.

Thanks,
-Kalyan