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
3,154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
chrismerritt earned 500 total points
ID: 36589847
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
ID: 36589978
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
ID: 36590292
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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