?
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
Medium Priority
?
3,808 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

765 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