Solved

PowerShell script to find a string and delete all lines in a text file that do not have it

Posted on 2011-09-23
3
2,182 Views
Last Modified: 2012-05-12
I am wondering if it's possible to create a powershell script to do the following:

Find a value of lets say 123 in a text file

The value of 123 is in Line 2 of the text file but not Line 1 or 3.

I want it to delete line 1 and 3 but save line 2.

This would need to be done for thousands of lines.

Is this possible? Powershell or DOS, VB?

thanks in advance
0
Comment
Question by:awgit
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
chrismerritt earned 500 total points
Comment Utility
Try this, but please test it first. i.e backup the file you are testing against.

Will overwrite the specified file content with any lines that contain the word in the $match variable. i.e. it will only keep lines that match your requirements, all other lines will be removed.

#Customise Vars
$SourceFile = "C:\TEMP\test.txt"
$Match = "123"

(Get-Content $SourceFile) | % {if ($_ -match $Match){$_}} | Set-Content $SourceFile

Open in new window

0
 

Author Comment

by:awgit
Comment Utility
That worked!

As I was testing myself I just found another command that also worked. Instead of modifying the original file it exports out the results to another file

This is the command that does that:
get-childitem C:\scripts\source.txt | select-string -pattern 123 | foreach {$_.line} > C:\scripts\123.txt
0
 
LVL 9

Expert Comment

by:chrismerritt
Comment Utility
No problem, you could save to another file by modifying my code as follows as well:

#Customise Vars
$SourceFile = "C:\TEMP\test.txt"
$DestinationFile = "C:\TEMP\new.txt"
$Match = "123"

(Get-Content $SourceFile) | % {if ($_ -match $Match){$_}} | Set-Content $DestinationFile

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now