The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleting lines in text file hern14 UNIX for Dummies Questions & Answers 5 05-16-2008 12:03 PM
Deleting lines in a file reachsamir Shell Programming and Scripting 2 05-29-2007 08:17 AM
Deleting last 2 lines from the file. videsh77 Shell Programming and Scripting 4 04-06-2007 10:14 AM
Deleting Lines from .csv file 009satya Shell Programming and Scripting 3 11-13-2006 11:41 PM
deleting lines in a log file BG_JrAdmin Shell Programming and Scripting 2 08-22-2006 08:12 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-21-2005
Registered User
 

Join Date: Sep 2005
Posts: 5
Question Deleting lines from a file

How I can delete 100 lines anywhere in a file without opening a file and without renaming the file.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-21-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,317
I could think of this:
Code:
sed -n "${to},${from}p" file_to_cut_lines_from | cat > file_to_cut_lines_from
You can use this to get a block of lines from a file, deleting the rest of the lines (because of the >).

This works in ksh, but is not portable in sh and bash. This has to do with the way ksh treats the '>'. There is a post where Perderabo has given an explaination for this - but I cant seem to remember where that post is...

Last edited by blowtorch; 09-21-2005 at 09:58 AM.
Reply With Quote
  #3 (permalink)  
Old 09-21-2005
Registered User
 

Join Date: Sep 2005
Posts: 5
Actually I have 8GB file which contains 200,000 records and I want to delete first 50,000 records without opening the file. At the same time I don't want to create another file once I delete the records because of space problem on server.

Is there any way I can delete 50,000 lines from 'abc.dat' and redirect output in the 'abc.dat' only.
Reply With Quote
  #4 (permalink)  
Old 09-21-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
ed can do this but it does create a temp file.
Reply With Quote
  #5 (permalink)  
Old 09-21-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,317
Records are potentially very different from lines. You could have your own delimiters and have a record spanning several lines. If each line is a single record, then you could still run the following command under ksh:
Code:
sed -n "50000,200000p" file_to_cut_lines_from | cat > file_to_cut_lines_from
Though I really dont know if you can do this with such a large file. Even if it works, it will take a really long time to get through the file.
Reply With Quote
  #6 (permalink)  
Old 09-21-2005
Registered User
 

Join Date: Sep 2005
Posts: 5
It's not working....it's deleting the whole file..
Reply With Quote
  #7 (permalink)  
Old 09-21-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,317
Are you running this under ksh? It will *only* work under ksh and *not* under bash and sh.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0