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,377 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
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

932 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

12 Experts available now in Live!

Get 1:1 Help Now