LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2011, 05:47 AM   #1
p1xty
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Rep: Reputation: Disabled
Removing all but last 5 lines


Hi,

I want to be able to remove everything but the last five lines of a .txt file. I looked at a sed tutorial but I was only able to figure out a way to remove all lines but the last but I need the last five lines. The file size is variable so specifying line numbers isn't possible. Any suggestions would be appreciated.
 
Old 11-03-2011, 05:49 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"man tail"
 
Old 11-03-2011, 05:51 AM   #3
p1xty
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Tail...of course! Thanks.
 
Old 11-03-2011, 05:53 AM   #4
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
If you want to do this through a script or command line, you can do it with tail.

Code:
tail -n 5 $oldfile > $newfile
will create a new file containing only the last 5 lines

Code:
tail -n 5 $oldfile > $newfile && mv $newfile $oldfile
will replace the original with the new file, effectively removing the all but the last 5 lines.

See "man tail" for further information..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing new lines from a file psandeepnair1985 Programming 5 03-25-2007 11:46 AM
removing special lines from files Prasun1 Linux - General 3 09-11-2005 05:16 AM
Removing duplicate lines with sed tireseas Programming 10 01-12-2005 03:27 AM
removing lines from file script iluvatar Programming 9 08-20-2004 05:49 AM
Removing lines from file Aylar Programming 2 04-22-2004 06:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration