0

Using PowerShell I am able to get the lines from a file which contain the needed text:

Get-ChildItem -recurse -Attributes !Directory+!System | Get-Content | Select-String "tshtml"

This gives the result below:

 templateUrl: '/tshtml/generic/gkeyvalue/gkeyvalue.html'
 templateUrl: '/tshtml/generic/permission/permission.html'
 templateUrl: '/tshtml/generic/permission2/permission2.html'

But now I want part of this as output:

 /generic/gkeyvalue/gkeyvalue.html
 /generic/permission/permission.html
 /generic/permission2/permission2.html

How do I do that? I am reading about regex, but I am not getting much about it :(

0

2 Answers 2

1
... | Select-String "tshtml" | ForEach { $_.Line -replace "^.*(?=/generic)|'$" }

To replace the start of a line leading up to /generic, and the ' from the end of the line, with nothing.

or

Select-String "tshtml" | ForEach { if ($_.Line -match "'/tshtml(.*)'") { $Matches[1] } }

To capture the text between the '' and output it.

Sign up to request clarification or add additional context in comments.

2 Comments

Even simpler for the section option: ...|Get-Content|?{$_ -match "'/tshtml(.*)'"}|%{ $Matches[1]} There's no need for the Select-String and then matching, just do it all at once.
@TheMadTechnician yeah. Or use select-string to do it all at once ... | Select-String "'/tshtml(.*)'" | ForEach {$_.matches[0].groups[1].Value}
0

Try the below:

... | Select-String "tshtml"|awk '{print $2}'| tr -d "'"

2 Comments

Assuming you have a Windows build of awk and tr to use, the output of Select-String converted to plain string includes the filename as well, so this would need to be {print $3} to get the right output.
I know i could do it easily using sed/awk but not aware about how to do it on windows :( and I dont have windows build of sed/awk/tr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.