Powershell select-string substring

狂风中的少年 提交于 2019-12-12 03:57:46

问题


I am currently reading the contents of a file and returning a pattern which gets one line. I am able to get the correct line by itself now. How do I do a substring function on the $RunTimeLine result to return the first four characters?

[string] $RunTimeLine = Select-String -Path $IDCSwiftFlowXMLFile -pattern "<time>" | ForEach-Object {$_.Line}

OUTPUT of $RunTimeLine >>>>>>> 30 5 * * 2-6 2

I have attempted doing the following but it isn't returning anything

$StartTime = $RunTimeLine.substring(0,4)

Any help is greatly appreciated. Thanks.


回答1:


The String.Substring method should work IF you have a string. If you execute:

$StartTime = $RunTime.substring(0,4)

it doesn't return anything because the result is assigned to $StartTime. What does this output:

$StartTime

If nothing then try $StartTime -eq $null. Is it possible the first four chars are whitespace so that it appears there is no output. You could check that with $StartTime.Length.



来源:https://stackoverflow.com/questions/14588279/powershell-select-string-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!