LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2011, 09:49 PM   #1
drexnefex
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
Delete last 6 lines of file with sed


looking to delete the last 6 lines of a file with sed.

figured out how to delete the last line..but i want the last six.

sed '$d' foo > bar
 
Old 06-05-2011, 10:01 PM   #2
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Well if you know the number of lines you can use some thing like (example is 66 lines):
Code:
sed '60,66 d' < filename
also you can find out linenumbers with
Code:
wc -l filename

Last edited by micxz; 06-05-2011 at 10:04 PM.
 
Old 06-06-2011, 12:54 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,458

Rep: Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852
Maybe you need to have a look at the right tool, try either tail or head.
 
Old 06-06-2011, 05:58 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi drexnefex,

Check is this Okay ?

Code:
tac filename | sed '1,6 d' | tac > filename
Thanks,
 
1 members found this post helpful.
Old 06-06-2011, 06:07 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
From USEFUL ONE-LINE SCRIPTS FOR SED by Eric Pement:
Code:
 # delete the last 10 lines of a file
 sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
 sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 
Old 06-06-2011, 07:21 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by drexnefex View Post
looking to delete the last 6 lines of a file with sed.

figured out how to delete the last line..but i want the last six.

sed '$d' foo > bar
Unless you really have to use sed, I would recommend tail:

Code:
tail -n6 < foo > bar
 
Old 06-06-2011, 10:44 AM   #7
drexnefex
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
awesome, thanks all.

this is plenty of info for me to chew on for now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM
sed: delete lines after last occurrence of a pattern in a file zugvogel Programming 4 11-17-2009 01:49 AM
sed delete lines from file one if regexp are listed in file two fucinheira Programming 6 09-17-2009 08:28 AM
[sed || gawk]: find and delete blocks and lines from file Hisu Programming 1 09-16-2008 02:01 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM


All times are GMT -5. The time now is 04:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration