I want find string using dictionary file

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


August 24th, 2015 4:15am

Hi,

Take a look at Select-String:

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

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 8:20am

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
		}
		
	}
	
}


August 24th, 2015 10:35am

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

 
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 6:22pm

thank you. your answer it's work.
August 24th, 2015 11:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics