Avatar of bibi92
bibi92
Flag for France asked on

build list exclude FS

Hello,

I parse a txt file $FILE="d:\admin\exclude_fs.txt"
that contains a list with this following format :
d:\mssql\:FS:OFF
i:\backup\:FS:OFF

How can I build a list EXCLUDE_FS?

I try this :
$Global:EXCLUDE_FS= Select-String -Path $FILE '^(\w+):FS:OFF' | foreach {$_.matches} | foreach {$_.groups[1].value}

But it doesn't work. How can I modify the command line?

Thanks

bibi
Powershell

Avatar of undefined
Last Comment
footech

8/22/2022 - Mon
SubSun

Just to be clear.. What do you expect as a result?
bibi92

ASKER
The result of echo $EXCLUDE_FS must be :
d:\mssql\
i:\backup\

Thanks
bibi92

ASKER
I have found this solution :
$EXCLUDE_FS=get-content $FILE | where-object {$_.contains(":FS:OFF")} |Foreach-Object {$
_ -replace ":FS:OFF", ""}
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Mazdajai

Or simpler -
gc "exclude_fs.txt"|%{
$_ -replace '^(.*?):FS:OFF', '$1'
}

Open in new window

ASKER CERTIFIED SOLUTION
SubSun

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Qlemo

The reason your original code did not work is
a) (\w)+ will only match words - colon, backslash etc. are not allowed. The RegEx cannot find the lines for that reason.
b) The Matches property for Select-String does not work as for other cmdlets, it only contains a single match (Matches[0]) , which is the complete pattern. So even if the lines were found, you will still get the whole line and have to remove the :FS:OFF part.

http:#a39045887 is the "best" way to get your desired result.
(http:#a39045873 will just output all lines, stripped off the :FS:OFF part if existing.)
footech

Just browsing through some old questions, and wanted to point something out for future posterity.

Qlemo is absolutely correct about reason "a", the RegEx wasn't correct for what was trying to be matched.  However, reason "b" is not valid.  The Matches property includes a Groups property, which will include both the complete pattern and any captured groups from the RegEx.  In this case $_.groups[1].value would show the value of that captured group.  The code would work just fine with proper RegEx.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.