The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete line in text file Berserk UNIX for Dummies Questions & Answers 6 12-16-2008 03:44 PM
how to delete text from line starting pattern1 up to line before pattern2? repudi8or Shell Programming and Scripting 5 04-15-2008 09:25 PM
how to delete line with matching text and line immediately after orahi001 UNIX for Dummies Questions & Answers 6 01-15-2008 12:34 AM
Delete first line from any text file ? aungomarin Shell Programming and Scripting 5 05-16-2006 09:42 PM
how to delete away text in a file? forevercalz Shell Programming and Scripting 5 01-08-2006 10:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-30-2002
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
delete last line from text file

I have a file with which i must remove the last line of the file (the footer). I know how to copy and redirect with tail -1 etc, but how do i delete it permanentely
  #2 (permalink)  
Old 05-30-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
My way to delete the last line of a file is with sed...

sed '$d' < input

Of course, to make this permanent, you need something like:

sed '$d' < input > tempfile
mv input input.old
mv tempfile input
  #3 (permalink)  
Old 06-04-2002
LANkrypt0 LANkrypt0 is offline
Registered User
  
 

Join Date: Jul 2001
Location: NJ
Posts: 3
Same as Perderabo's but a one liner

sed '$d' < file1 > file2 ; mv file2 file1
  #4 (permalink)  
Old 06-25-2002
Nisha Nisha is offline
Registered User
  
 

Join Date: Jun 2002
Location: Chennai, India
Posts: 110
Hi,

What does '$d' signify here? What if i want to delete the second last line. Do i have to use '$2d'?

Can you clarify?

Thanks,
Nisha
  #5 (permalink)  
Old 06-25-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
$ means the last line. But sed doesn't know that it has the last line until it arrives. You can't do a "sed '$-1d'" or anything. Deleting the next to the last line via sed is very hard. So hard that I would normally use other techniques. But just for the heck of it, I finally got a sed script running that deletes the next to the last line:
Code:
#! /usr/bin/sed -nf
#
${P
q
}
1{h
d
}
x
p
And this solution will not easily scale up. A sed script to delete $-2 will take about 10 times the code. Deleting $-3 would be a nightmare. The problem is that you don't know in advance how many lines there are. To really have a general solution, you need to make two passes through the file. On the first pass, you count the lines. On the second pass, you delete the appropriate line. sed cannot do this alone since it always will only make a single pass through the file.
Code:
#! /usr/bin/ksh
lines=$(wc -l $1)
((target=lines-1))
sed "${target}d" < $1
exit 0
Now we have an extensible solution.
Bits Awarded / Charged to Perderabo for this Post
Date User Comment Amount
09-16-2009 slarionoff Fine solution! 1,000
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0