How do I continue to get the 12345 as output?
file.txt
MyText: 12345
myscript
Get-Content file.txt | Select-String "MyText:" | Select-Object Line
Now I would like to save last five characters, how to I retreive them?
CodePudding user response:
The Select-String cmdlet -Pattern
parameter expects a regex - so you can get your desired output by capturing the five digits. Note that the Select-String
cmdlet also takes a -Path
parameter to retrieve the content of a file. This means you can omit the first Get-Content
command:
(Select-String -Path .\file.txt -Pattern 'MyText: (\d{5})').Matches.Groups[1].Value