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

Is Your AD Toolbox Looking More Like a Toybox?

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

Suggested Solutions

Title # Comments Views Activity
Powershell + stopping and starting on a remote server 12 23
Exchange powershell help 4 21
powershell md/mkdir/New-item   -Quiet 10 38
Clear Filter 8 22
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A procedure for exporting installed hotfix details of remote computers using powershell
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

11 Experts available now in Live!

Get 1:1 Help Now