Home > Software engineering >  Substring of each line of some data in a file using powershell
Substring of each line of some data in a file using powershell

Time:08-25

I'm working with an Android log file, In the logs we are pushing some data that may be used as a training set.

This is roughly how the relevant logs look

[Timestamp] : [Function name] : 1.1 1.2 1.3...
[Timestamp] : [Function name] : 2.1 2.2 2.3...

I want to trim this data to just the floating point numbers at the end, so that we can convert it to csv.

This is what I tried

Get-Content .\log.txt | Select-String "Function Name" | foreach {$_.substring(73)} 

I'm getting an error that powershell doesn't have a method named substring

CodePudding user response:

If really you'd like to follow this way - you can just enforce string

gc .\log.txt |Select-String "function name"  | % {([string]$_).substring(73) }

before adding [string]

Name BaseType


MatchInfo System.Object

after adding [string] Name BaseType


String System.Object

and of course you can substring string :)

  • Related