Inserting/Deleting newline(s) in very large text files

Dlanor Slegov dlanorslegov at rocketmail.com
Fri Aug 13 07:14:37 EDT 2010


I would greatly appreciate a python solution as this problem is only the head of 
a larger problem for which I am on a mission to write a FULL one-stop-shop 
python script. I am trying two things- iter() and fileinput module...
Thanks!


________________________________
From: Matty Sarro <msarro at gmail.com>
To: Dlanor Slegov <dlanorslegov at rocketmail.com>
Sent: Thu, August 12, 2010 5:22:58 PM
Subject: Re: Inserting/Deleting newline(s) in very large text files


On Thu, Aug 12, 2010 at 11:12 AM, Dlanor Slegov <dlanorslegov at rocketmail.com> 
wrote:

Hi,
>
>I am dealing with very large text files (a few million lines) and would like to 
>check and modify them according to a well defined format. The format 
>requires ONLY ONE NEWLINE (followed by some sort of text) on top of the file and 
>NO NEWLINE in the very end. The input files can be very diverse, such as one 
>file may have 2 newlines on top and none in the end or other may not have a 
>newline on top and 5 in the end. 
>
>
>The aim is to check these files & introduce JUST ONE NEWLINE in the TOP and NO 
>NEWLINE in the BOTTOM.
>
>Any pointers will be appreciated.
>
>Thanks,
>DS.
>  
>
>--
>http://mail.python.org/mailman/listinfo/python-list
>
>
If you're using a unix box you may want to look into sed.
sed -i.<extensionofbackup> '1i text to be inserted in first line' <filename>

For example:
sed -i.bak '1i alias bond0 bonding' /etc/modprobe.conf
Would back up the original file to the initial filename plus the extension .bak. 
Then it would insert:
alias bond0 bonding
into /etc/modprobe.conf

Into the 1st line of the file, moving everything else down one line.

You can find some info on dealing with newlines here:
http://www.grymoire.com/Unix/Sed.html#toc-uh-nl

I know you're looking for a python solution, but this may be easier. Just a 
heads up.



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100813/ecc2444b/attachment.html>


More information about the Python-list mailing list