![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Using RegEx to get an exact match I'm working on a PowerShell script that will clean up a file that has 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 to). For example: fileA: userA userAA userB userBB fileB: userA userB My script looks like this: ##################### # Cleaning up the list so all entries are sorted, unique, and in lower case $moveList = get-content fileA.txt | sort-object -uniq | foreach-object { $_.ToLower() } # Cleaning up the list so all entries are sorted, unique, in lower case, and if they put in the domain portion of the account # I strip that out. $exclude = get-content fileB.txt | sort-object -uniq | foreach-object { $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"} # Go through each object in $exclude and remove any matching object in $moveList $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 moves. $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 $_, it is matching: userA userAA 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. |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Using RegEx to get an exact match Not sure I'm following you. Based on FileA and FileB, What would be the content of CleanUserList.txt? ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Using RegEx to get an exact match a b 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 the content of CleanUserList.txt? ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Using RegEx to get an exact match I'm sure there's a shorter way , until then:$moveList = get-content fileA.txt | sort-object -uniq | foreach-object { $_.trim() } $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 ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | 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
| ||||||||||||||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Using RegEx to get an exact match I think there's some confusion :-). 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 :-). 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
| ||||||||||||||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Using RegEx to get an exact match $moveList = get-content fileA.txt | sort -unique get-content fileB.txt | sort -unique | foreach { $moveList = $moveList -replace "^$_`$" } $moveList | where {$_} | out-file -encoding ASCII .\CleanUserList.txt ### CleanUserList.txt ### blee c_tlee ulee ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||||||||||||||||||||||||||
| | #8 (permalink) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | Re: Using RegEx to get an exact match 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||