-2

i have this in a log file, and i want only the time without (s) in a new log file or put in variable, thanks for your reponse.

Fri Nov 02 13:47:07 2018  time =5.803s
Fri Nov 02 14:02:10 2018  time =7.082s 

My Code

Get-Content "logfile.txt" |
    select-string -pattern "5" |
    foreach-object{
        write-Output $_ >> "C:\Users\titi\Desktop\test.txt"
    }
ArcSet
  • 6,518
  • 1
  • 20
  • 34
jack
  • 67
  • 9
  • What have you come up with so far? Show us your code and we'll be happy to help you to make it work. – TobyU Nov 05 '18 at 16:11
  • my code is Get-Content "logfile.txt" | select-string -pattern "5" | foreach-object{ write-Output $_ >> "C:\Users\titi\Desktop\test.txt"}, i know that take only line with 5 but i want to take line superior at 5 – jack Nov 05 '18 at 16:16
  • Just guessing with superior line you mean the following line - take a look at he `-context` parameter of Select-String. You should [edit] your question to contain the exact output you expect. –  Nov 05 '18 at 17:40

1 Answers1

1

this will get the info you seem to want ... [grin]

# fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
Fri Nov 02 13:47:07 2018 time =5.000s
Fri Nov 02 13:47:07 2018 time =5.803s
Fri Nov 02 14:02:10 2018 time =7.082s
Fri Nov 02 13:47:07 2018 time =4.000s
Fri Nov 02 13:47:07 2018 time =3.000s
Fri Nov 02 13:47:07 2018 time =5.001s
'@ -split [environment]::NewLine

$MinimumSeconds = 5.0

$Results = foreach ($IS_Item in $InStuff)
    {
    $Seconds = [decimal]($IS_Item.Split('=')[1].Trim().TrimEnd('s'))
    if ($Seconds -gt $MinimumSeconds)
        {
        $Seconds
        }
    }

''
$Results

output ...

5.803
7.082
5.001

what happened above ...

  • split on the '='
  • take the 2nd part of the resulting array
  • trim away any leading or trailing whitespace
  • trim away the ending 's'
  • convert to a [decimal] value
  • compare to the required minimum
  • send the remaining number to the $Results collection
  • display it

saving it back to a file would use Set-Content. [grin]

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • if i have 4.000s that can display but i want superior at 5 – jack Nov 05 '18 at 16:32
  • @jack - what do you mean by "superior at 5"? i have no idea what that means ... please, try to rephrase it. – Lee_Dailey Nov 05 '18 at 16:35
  • if i have this value : Fri Nov 02 13:47:07 2018 time =5.803s Fri Nov 02 13:47:07 2018 time =4.000s Fri Nov 02 14:02:10 2018 time =7.082s i want only a value superior at 5.000s so i want 5.803 and 7.083, thanks – jack Nov 06 '18 at 08:08
  • @jack - ah! so you want the values _greater than_ 5.0 ... i will post a way to do that in my original answer in a while. [*grin*] – Lee_Dailey Nov 06 '18 at 15:03
  • @jack - take a look at the current version - i added a "coerce from string to number" & then a comparison to the required minimum. – Lee_Dailey Nov 06 '18 at 15:11