Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag 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
Avatar of SubSun
SubSun
Flag of India image

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

ASKER

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

Thanks
Avatar of bibi92

ASKER

I have found this solution :
$EXCLUDE_FS=get-content $FILE | where-object {$_.contains(":FS:OFF")} |Foreach-Object {$
_ -replace ":FS:OFF", ""}
Or simpler -
gc "exclude_fs.txt"|%{
$_ -replace '^(.*?):FS:OFF', '$1'
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.)
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.