CMD Prompt / PS / Batch - Find Unique Values in a .TXT File

Page 3 of 3 FirstFirst 123

  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #21

    Brilliant @das10,

    That is SOO much better than my LONG brute force approach. I tested it out and it gives EXACTLY what I wanted.

    I have tweaked it slightly and added a Total . . .

    Code:
    
    $Source_File = 'C:\Users\System-Admin\Desktop\System_Reports.bat'
    $Destination_File = 'C:\Users\System-Admin\Desktop\ZZZ.txt'
    $Find_This = '\%~{0,1}\w{1,50}~{0,1}\%'
    $Find_Also = '\!~{0,1}\w{1,50}~{0,1}\!'
    
    if (Test-Path $Destination_File) {Remove-Item $Destination_File}
    
    Select-String -Path $Source_File -Pattern $Find_This -AllMatches | % { $_.Matches } | % { $_.Value } | Sort | Unique | Out-file -FilePath $Destination_File
    
    Select-String -Path $Source_File -Pattern $Find_Also -AllMatches | % { $_.Matches } | % { $_.Value } | Sort | Unique | Out-file -FilePath $Destination_File -Append
    
    (Get-Content -Path $Destination_File).Length | Out-file -FilePath $Destination_File -Append
    

    EDIT:

    I extended the Parameter to \w{1,50} to include some of the built-in wmic variables, and it worked GREAT!

      My Computer


  2. Posts : 3,274
    Win10
       #22
    Last edited by das10; 06 Oct 2021 at 11:45.
      My Computers


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #23

    das10 said:
    I am glad to see you are having more fun with this script (toy).
    For more RegEx also see: . . .
    I actually have those and a few others Bookmarked for future use if needed.

    Last edited by Paul Black; 06 Oct 2021 at 11:42.
      My Computer


  4. Posts : 3,274
    Win10
       #24

    I don't know if it might be worth experimenting more if symbols like ~ # + - (etc.) are also needed to be included at 'any position' between two % signs, instead of just the tilde at the beginning/end as shown in original RegEx.

    Code:
    $findthis='\%(\w{1,1}|[~#+-]{1,}){1,50}\%'
    $findalso='\!(\w{1,1}|[~#+-]{1,}){1,50}\!'
    Last edited by das10; 08 Oct 2021 at 17:24.
      My Computers


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #25

    Hello @das10,

    das10 said:
    I don't know if it might be worth experimenting more if symbols like ~ # + - (etc. but not space-which would break this particular RegEx) are also needed to be included at 'any position' between two % signs, instead of just the tilde at the beginning/end as shown in original RegEx.

    Code:
    $findthis='\%(\w{1,1}|[~#+-]{1,}){1,50}\%'
    $findalso='\!(\w{1,1}|[~#+-]{1,}){1,50}\!'

    Excellent.

    I tested it with ALL of ONE symbol, and then a combination of symbols [ at the front, middle, & end ] within the %, and they ALL Worked perfectly.

    I forgot to mention that the Underscore which I use, ALSO works in the original and updated version. I use the Underscore in some of my Variables because it separates them from the Environment Variables, which mainly have a Capital letter for each word [ e.g. %NumberOfProcessors% ] . . .

    Code:
    
    $Source_File = 'C:\Users\System-Admin\Desktop\System_Reports.bat'
    $Destination_File = 'C:\Users\System-Admin\Desktop\Extracted_Variables.txt'
    $Find_This = '\%(\w{1,1}|[~#+-]{1,}){1,50}\%'
    $Find_Also = '\!(\w{1,1}|[~#+-]{1,}){1,50}\!'
    
    if (Test-Path $Destination_File) {Remove-Item $Destination_File}
    
    Select-String -Path $Source_File -Pattern $Find_This -AllMatches | % { $_.Matches } | % { $_.Value } | Sort | Unique | Out-file -FilePath $Destination_File
    Select-String -Path $Source_File -Pattern $Find_Also -AllMatches | % { $_.Matches } | % { $_.Value } | Sort | Unique | Out-file -FilePath $Destination_File -Append
    
    (Get-Content -Path $Destination_File).Length | Out-file -FilePath $Destination_File -Append

    As an added BONUS, extracting the Variables is also a good way of checking that you have ALL the Variables correct. If you use the SAME Variable, and use a Capital letter in one, and NOT in another, it will extract BOTH Variables, therefore making you aware that there is a difference somewhere. Sorry, it is just my OCD.

    Last edited by Paul Black; 08 Oct 2021 at 08:55.
      My Computer


  6. Posts : 3,274
    Win10
       #26

    Just a note;
    Interestingly according to Microsoft the underscore character is part of the alphanumeric (\w) description given here.
    Alphanumeric variable names can contain these characters:
    https://docs.microsoft.com/en-us/pow...powershell-7.1
    Unicode characters from these categories: Lu, Ll, Lt, Lm, Lo, or Nd.
    Underscore (_) character.
    Question mark (?) character.
      My Computers


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #27

    Hello @das10,

    das10 said:
    Just a note;
    Interestingly according to Microsoft the underscore character is part of the alphanumeric (\w) description . . .

    Interesting. I have used the Underscore for years in my BAT & CMD files for the reasons given above. Obviously there are going to be instances when using PowerShell where certain characters etc will NOT work or be accepted like they are in BAT & CMD files.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:01.
Find Us




Windows 10 Forums