Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Looking to delete last 3 lines with powershell

Reply
 
Old 10-29-2008   #1 (permalink)
DUHAAS


 
 

Looking to delete last 3 lines with powershell

I want to delete the last three lines of a file using powershell, there
doesnt seem to be an easy answer????

My System SpecsSystem Spec
Old 10-29-2008   #2 (permalink)
RickB


 
 

Re: Looking to delete last 3 lines with powershell

On Oct 29, 4:16*pm, DUHAAS <DUH...@xxxxxx> wrote:
Quote:

> I want to delete the last three lines of a file using powershell, there
> doesnt seem to be an easy answer????
You are almost going to need to stick something like this in your
pipeline

% -begin {$a,$b,$c=$null
}-process {$a=$b;$b=$c;$c=$_;if ($a){$a}}

This doesn't start emitting objects until you've read 3.
Since the pipeline stops with the contents of $b, $c and $_
having never been emitted they dissappear.

You could abstract the concept to use a FIFO Stack rather than hard
coding the size if you need more flexibility.
My System SpecsSystem Spec
Old 10-29-2008   #3 (permalink)
Shay Levy [MVP]


 
 

Re: Looking to delete last 3 lines with powershell

Hi DUHAAS,

This will read a file content (assuming it has more then 3 lines), and output
every line except for the last three:

$content = get-content file.txt
$content[0..($content.length-4)]


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



D> I want to delete the last three lines of a file using powershell,
D> there doesnt seem to be an easy answer????
D>


My System SpecsSystem Spec
Old 10-29-2008   #4 (permalink)
Kryten


 
 

Re: Looking to delete last 3 lines with powershell

Hi,
Just one way, I'm sure there will be more correct, faster, beautiful
ways to do it.

Anyway, in this one-liner hh.txt is your starting file and hh2 is the
same but less
the last three lines. Nothing stopping you overwriting the start file,
if thats what you want.

Seems to work ok on several test text files I played around with.

$a = gc hh.txt; $c = $($a.Count -1) ; $c..$($c -2) | % {$a[$_] = Out-
Null } ; sc -path hh2.txt -va $a

Hope it helps,
Stuart











On 29 Oct, 21:16, DUHAAS <DUH...@xxxxxx> wrote:
Quote:

> I want to delete the last three lines of a file using powershell, there
> doesnt seem to be an easy answer????
My System SpecsSystem Spec
Old 10-30-2008   #5 (permalink)
RickB


 
 

Re: Looking to delete last 3 lines with powershell

On Oct 29, 5:02*pm, RickB <rbiel...@xxxxxx> wrote:
Quote:

> On Oct 29, 4:16*pm, DUHAAS <DUH...@xxxxxx> wrote:
>
Quote:

> > I want to delete the last three lines of a file using powershell, there
> > doesnt seem to be an easy answer????
>
> You are almost going to need to stick something like this in your
> pipeline
>
> % -begin {$a,$b,$c=$null
> *}-process {$a=$b;$b=$c;$c=$_;if ($a){$a}}
>
> This doesn't start emitting objects until you've read 3.
> Since the pipeline stops with the contents of $b, $c and $_
> having never been emitted they dissappear.
>
> You could abstract the concept to use a FIFO Stack rather than hard
> coding the size if you need more flexibility.
I noticed the other solutions assume the data source is small enough
to be loaded into memory. That's probably true in many cases. I
typically deal with files in the 100's of MB so I'm alwasy thinking
BIG. This tosses the last X records in a stream.

$X = 3

<data src>|% -begin{$q = new-object system.collections.queue
}-process{$q.enqueue($_);if ($q.count -gt $X){$q.dequeue()}|
<data dest>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Batch Commands To Read and Delete Lines from txt-File PowerShell
Sample script to delete lines from a file based on a string PowerShell
Delete Files using powershell PowerShell
How to delete a DFS node using Powershell PowerShell
delete temp dlls via powershell PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46