Re: Using RegEx to get an exact matchAnother method I was considering was a hash table.
$hash = @{}
gc exclude.txt | %{$hash.$_ = $_}
gc fulllist.txt | %{if (-not $hash.containsvalue($_)) {$_}} > cleanlist.txt
Cheers,
Adam.
"Steven" <evetsleep@xxxxxx> wrote in message news:000001c8627f$ee39e780$0202fea9@xxxxxx
I think there's some confusion J. Let me change to include more realistic names.
fileA.txt:
blee
c_tlee
ulee
lee
fileB.txt:
c_smith
lee
radams
In fileB one of the entries is "lee". This will match all 3 entries in fileA.txt and with the -replace operator is results in:
b
c_t
u
Remember that what I'm trying to do is take a file that has a list of user names and remove the ones that are in fileB from fileA. The above ends up corrupting the user name.which isn't what I'm trying to do J. What the results should look like (if I can get this to work) is:
blee
c_tlee
ulee
Where it just removes "lee".
_____________________________________________
From: Shay Levi [mailto:no@xxxxxx]
Posted At: Monday, January 28, 2008 6:48 PM
Posted To: microsoft.public.windows.powershell
Conversation: Using RegEx to get an exact match
Subject: Re: Using RegEx to get an exact match
Modified version:
$moveList = get-content fileA.txt | sort -unique
get-content fileB.txt | sort -unique | foreach { $moveList = $moveList -replace
$_ }
$moveList | where {$_ -ne ""} | out-file -encoding ASCII ..\CleanUserList.txt
A
B
-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> I'm sure there's a shorter way
, until then:
> $moveList = get-content fileA.txt | sort-object -uniq | foreach-object
> $exclude = get-content fileB.txt | sort-object -uniq | foreach-object
> $_.trim() ; $moveList = $moveList -replace $_ }
> $moveList = $moveList -match "^\w+"
> $moveList | out-file -encoding ASCII .\CleanUserList.txt
>> So the problem that I'm having is that in $_ is "userA" and it's
>> matching both "userA" and "userAA". I only want it to match "userA".
>> -----Original Message-----
>>> From: Shay Levi [mailto:no@xxxxxx]
>>> Posted At: Monday, January 28, 2008 5:37 PM
>>> Posted To: microsoft.public.windows.powershell
>>> Conversation: Using RegEx to get an exact match
>>> Subject: Re: Using RegEx to get an exact match
>>> Not sure I'm following you. Based on FileA and FileB, What would be
>>> of CleanUserList.txt?
>>> I'm working on a PowerShell script that will clean up a file that
>>> a list of names to be moved to Exchange 2007 based off an exclusion
>>> list. This is because I have some mailboxes that I don't want moved
>>> (for one reason or another). So what I want to do is take fileA and
>>> search through it for any user names in fileB and pull them out and
>>> then save the results as a "clean" file.
>>> The problem I've run into is that when using the -replace regex ,
>>> it's not performing exact matches (at least as I had expected it
>>> My script looks like this:
>>> #####################
>>> # Cleaning up the list so all entries are sorted, unique, and in
>>> $moveList = get-content fileA.txt | sort-object -uniq |
>>> # Cleaning up the list so all entries are sorted, unique, in lower
>>> and if they put in the domain portion of the account
>>> $exclude = get-content fileB.txt | sort-object -uniq |
>>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
>>> # Go through each object in $exclude and remove any matching object
>>> $exclude | foreach-object { $moveList = $moveList -replace $_ }
>>> # Clean up whitespace left by replace operation
>>> $moveList = $moveList -match "^\w+"
>>> # Output clean file to file to be used as an answer file for mailbox
>>> $moveList | out-file -encoding ASCII CleanUserList.txt
>>> #####################
>>> The problem that I'm seeing is that "$moveList -replace $_" is not
>>> hitting exact matches. So in the above example if userA was in $_,
>>> What I'm left with is "A" because it is stripping out the "userA"
>>> part. How can I have it match only "userA" and not userAA" or better
>>> yet, does anyone have a better way doing what I'm doing?
>>> Thanks for any suggestions..as always I really appreciate it.