Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
Using PowerShell
>
General PowerShell
How to remove certain lines in text files?
Last Post 04 Aug 2010 10:46 AM by
absolutebeginner
. 4 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
absolutebeginner
New Member
Posts:14
02 Aug 2010 12:17 PM
Hello, I have around 35000 text files (ASCII) from which I need to delete the lines 2, 3, 4 and 5. How could I do this with a PS script? Thank you very much!
George Howarth
Basic Member
Posts:360
02 Aug 2010 12:34 PM
Try this (make sure you test it on just one file first :) ):
$path = "file.txt"
$lines = Get-Content -Path $path
1..4 | ForEach-Object { $lines[$_] = $null }
Set-Content -Path $path -Value $lines
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
02 Aug 2010 12:51 PM
Try this on a test file:
$file = get-content test.txt
$file[0],$file[5..($file.length-1)] | out-file test.txt -encoding ascii
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
PoSherLife
Basic Member
Posts:364
02 Aug 2010 12:57 PM
want another?
(gc file.txt) | ? {(2..5) -notcontains $_.ReadCount} | sc file.txt
When at first you don't succeed
Step-Into
http://theposherlife.blogspot.com
http://www.jandctravels.com
absolutebeginner
New Member
Posts:14
04 Aug 2010 10:46 AM
Thanks! I used Shays code. Worked perfectly.
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Active Directory
--Exchange Server
--Lync Server
--SharePoint
--SQL Server
--System Center
--Non-Microsoft Products
--Books, Tools, and Videos
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2