Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a substring from a text file?

Tags:

powershell

I have the following text file

testA=foo
testB=foobar
testC=Whatever

Now I want to extract the value for testB which is foobar. With my current PowerShell command I can only receive the whole line:

testB=foobar

My command is:

Select-String -Path .\test.txt -Pattern "testB=" -List

I could see that there is replace function for strings, so that you could replace testB= with empty string, but I do not know how to achieve this.

like image 678
Al Phaba Avatar asked Dec 14 '16 14:12

Al Phaba


Video Answer


1 Answers

You don't have to replace anything to capture the value. Instead, add a capture group to your search pattern. Then you are able to access the captured value:

(Select-String -Path $scripts.tmp -Pattern "testB=(.*)").Matches.Groups[1].Value
like image 102
Martin Brandl Avatar answered Sep 28 '22 07:09

Martin Brandl