The Linux Administration group is for the discussion of technical issues technical issues that arise during the administration of Linux systems, including maintaining the operating system and supporting end-user applications.

Edit text file.

I have a text file - is there a command that I can use to delete the first N number of lines, delete the last N number of lines, or a range of lines in the middle of the file? Note, I do not want to recreate the file using head|tail commands and I do not want to manully use vi - I need a command/switch that can be scripted. Thank you.

Download Messenger. Join the i’m Initiative. Help make a difference today.

On Wed, 18 Apr 2007, srm_oracleunix via linuxadmin-l wrote:
>
> I have a text file - is there a command that I can use to delete the
> first N number of lines, delete the last N number of lines, or a
> range of lines in the middle of the file? Note, I do not want to
> recreate the file using head|tail commands and I do not want to
> manully use vi - I need a command/switch that can be scripted.

To delete the first N lines:

tail +$(( N + 1 )) FILE

To delete the last N lines is more difficult. See the topntail
command from chapter 19 of my book. The scripts are available
online at:

Copyright 1998-2015 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.