Home > Back-end >  powershell script -Extraction of strings between two characters
powershell script -Extraction of strings between two characters

Time:08-27

"aaaaaa,OU=DistributionGroups||bbbb,OU=SecurityGroups||cccc,OU=Groups||"

"eeeee,OU=FW||ffff,OU=Test||cccc,OU=vpn||"

Greetings I have a .csv file with hundreds of lines (as shown in the example) I need to extract all strings, for each line starting with OU = and ending with double ||

this is the desired result:

DistributionGroups

SecurityGroups

Groups

FW

Test

vpn

CodePudding user response:

Use Select-String -AllMatches to find all occurrences of strings occurring between OU= and ||:

$strings = Get-Content path\to\file.txt |Select-String 'OU=(. ?)\|{2}' -AllMatches |ForEach-Object {
    $_.Matches |ForEach-Object {$_.Groups[1].Value}
} |Sort-Object -Unique

$strings will now contain the (unique) values extracted

CodePudding user response:

I take advantage of your availability If you had a file containing multiple fields

UserID;Manager;Local;Gruoup

srt567;fre567;roma;||aaaaaa,OU=DistributionGroups||bbbb,OU=SecurityGroups||cccc,OU=Groups||

rto345;try8589;milano;||eeeee,OU=FW||ffff,OU=Test||cccc,OU=vpn||

and I would like to have a list by UserID of all the OUs present in the row (keeping however Manager; Local;) all split over several lines, see example:

srt567;fre567;roma;aaaaaa,OU=DistributionGroups

srt567;fre567;roma;bbbb,OU=SecurityGroups

srt567;fre567;roma;cccc,OU=Group rto345;try8589;milano;ffff,OU=FW

rto345;try8589;milano;ffff,OU=Test

rto345;try8589;milano;cccc,OU=vpn

Thenks

  • Related