none
I want find string using dictionary file RRS feed

  • Question

  • Hello.

    I need some help about script.

    A.txt file has below text

    ==============================
    blablablablablabla (0001) blablablabla
    blablablablablabla (0001) blablablabla
    blablablablablabla (0201) blablablabla
    ==============================

    B.txt file has below text
    ==============================
    0001
    0030
    0020 ...
    more have number.
    ==============================

    If has same charater in () on A.txt file compare B.txt, save conent C.txt

    ex. get-content A.txt | compare-object B.txt | set-content C.txt <-----maybe like this script.

    plz help me


    Monday, August 24, 2015 8:12 AM

Answers

  • For matching one of multiple values, you can use regex alternation ('|') to do them all in one pass.

    $regex = (get-content b.txt) -join '|'
    
    Select-String a.txt -Pattern $regex |
    select -ExpandProperty Line |
    Set-Content c.txt

    You can also use the -match operator instead of Select-String if you just need the text of the matched lines:

    (Get-Content a.txt) -match $regex |
    Set-Content c.txt
    

     

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, August 24, 2015 10:19 PM
    Moderator

All replies

  • Hi,

    Take a look at Select-String:

    http://ss64.com/ps/select-string.html


    Monday, August 24, 2015 12:16 PM
  • You can reference the below code to write your own .

    $Avals=Get-Content C:\A.txt
    $Bvals=Get-Content C:\B.txt
    $Regex='(\d+)'
    foreach ($Aval in $Avals)
    {
    	$MatchValue= $Aval | Select-String -Pattern $Regex | Select-Object  {$_.Matches}
    	$Value=$MatchValue.'$_.Matches'.Value
    	foreach($Bval in $Bvals)
    	{
    		if($Value -eq $Bval)
    		{
    			$Value | out-file C:\C.txt -Append
    		}
    		
    	}
    	
    }


    Monday, August 24, 2015 2:32 PM
  • For matching one of multiple values, you can use regex alternation ('|') to do them all in one pass.

    $regex = (get-content b.txt) -join '|'
    
    Select-String a.txt -Pattern $regex |
    select -ExpandProperty Line |
    Set-Content c.txt

    You can also use the -match operator instead of Select-String if you just need the text of the matched lines:

    (Get-Content a.txt) -match $regex |
    Set-Content c.txt
    

     

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, August 24, 2015 10:19 PM
    Moderator
  • thank you. your answer it's work.
    Tuesday, August 25, 2015 3:26 AM
  • thank you. your answer it's work.
    Tuesday, August 25, 2015 3:26 AM