Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to delete rows of multiple text files based on string list from another file?

$
0
0

Hello

I have multiple comma delimited text files (each has over 1 million rows). Also I have one file called "delete.log" .

Structure of delete.log file is like this:

STRING1   
STRING2   
STRING3  
.  
.   
STRING N

Structure one of many txt files is like this: name1.txt :

text1,text2,text3,text4,STRING1            "entire row will be deleted" 
text1,text2,text3,text4,STRING1            "entire row will be deleted"
text1,text2,text3,text4,text5              
text1,text2,text3,text4,STRING2            "entire row will be deleted"     
text1,text2,text3,text4,STRING1            "entire row will be deleted"
text1,text2,text3,text4,text5                  

I'm looking for a effective way how to read each text string from file "delete.log" and if there is a match in row 5 with text string from delete.log file, entire row will be deleted. Also if there is a text file in a folder with a file name from delete.log like STRING1.txt, file will be deleted.

This code below just delete entire row from multiple text files if text string in column 5 is STRING1

$paths = Get-ChildItem '.\' -Filter '*.txt'
ForEach ($path in $paths) {
$pathtmp = "$path.tmp"

$sr = New-Object -TypeName System.IO.StreamReader -ArgumentList $path
$sw = New-Object -TypeName System.IO.StreamWriter -ArgumentList $pathtmp

Do {
 $line = $sr.ReadLine()
 $Column =  $line.split(",")


If ($Column[4] -ne "STRING1")  {
    $sw.WriteLine($line)
}
} Until ( $sr.EndOfStream )

$sr.close()
$sw.close()

Remove-Item $path
Rename-Item $pathtmp $path
}

Viewing all articles
Browse latest Browse all 6937

Trending Articles