Finding Keywords in a text file with PowerShell on different lines

317 Views Asked by At

My apologies if what I am writing is a little confusing.

I'm currently trying to write a PowerShell script that will search for a keyword in a text file. Something like this

select-string $inputFile -pattern $templateName | foreach {$_.Line} | out-file $outFile

The keyword I am looking for forms part of a block of records that must always come together. Each record begins with the same value and ends with the same value. Only the values inside each record is different as well as the record numbers between 0001 & 9999 may also be different.

"0001","ABC","KVB","ZZZ-01"

"0010","ABC","KVB","Today"

"0066","ABC","KVB","Sunday"

"9999","ABC","KVB","Goodbye"


"0001","ABC","KVB","TVY-01"

"0008","ABC","KVB","Sky Blue"

"0018","ABC","KVB","West Sun"

"9999","ABC","KVB","3 Weeks From Today"

So what I am trying to achieve with the script is search for the keyword TVY-01 and when found, copy 0001, 0008, 00018 and 9999 to a text file. In the sample above, theres 2 blocks, so I only need to copy the 4 records that relate to the block that TVY-01 was found. Because the only constant will be 0001 and 9999, I need to copy those lines plus all the lines in between

The idea is to scan for a common value in thousands of data records and each time it finds one, copies the full block of records into a single file.

I hope this explanation doesn't confuse anyone.

Thanks

DW

0

There are 0 best solutions below