![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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???? 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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. 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 Specs![]() |
![]() |
| 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 | |||