How to truncate few bytes of file
Hi All, I need to truncate last few bytes of file. these files are big in size. One idea is to write needed bytes to another file and delete the original file, but i am dealing with big files :( dont want to use truncate, it just truncating the size, all data is gone any pointers Thanks a b
Re: How to truncate few bytes of file
On Fri, May 20, 2011 at 03:40:35PM +0530, a b wrote: > Hi All, > > I need to truncate last few bytes of file. these files are big in size. > > One idea is to write needed bytes to another file and delete the original > file, but i am dealing with big files :( > > dont want to use truncate, it just truncating the size, all data is gone > > any pointers truncate()'s second parameter is the length to which you want to truncate. Did that not work for you? If not, I would suggest posting some example code that fails, because calling truncate() seems to be the correct approach here. -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Truncate Last few lines
Here i paste a perl script to delete last Two Lines. If you want delete more lines in a file you can specify it. use File::ReadBackwards; my $filename = 'test.txt'; my $Lines_to_truncate = 2; # Here the line to truncate is mean Remove only Last Two Lines my $bw = File::ReadBackwards->new( $filename ) or die "Could not read backwards in [$filename]: $!"; my $lines_from_end = 0; until( $bw->eof or $lines_from_end == $Lines_to_truncate ) { print "Got: ", $bw->readline; $lines_from_end++; } truncate( $filename, $bw->tell ); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: How to truncate few bytes of file
Hey Thanks all! I got it ;) open (FH, "+< $fname")|| die "\nFailed to open file $fname\n"; my $tmp=$fsize-$trunccount; seek(FH,$tmp,0); $addr = tell(FH) ; truncate(FH, $addr)|| die "\nFailed to truncate $file: $!"; close(fd); print "\nTruncate successful\n"; On Fri, May 20, 2011 at 6:07 PM, Paul Johnson wrote: > On Fri, May 20, 2011 at 03:40:35PM +0530, a b wrote: > > > Hi All, > > > > I need to truncate last few bytes of file. these files are big in size. > > > > One idea is to write needed bytes to another file and delete the original > > file, but i am dealing with big files :( > > > > dont want to use truncate, it just truncating the size, all data is gone > > > > any pointers > > truncate()'s second parameter is the length to which you want to > truncate. Did that not work for you? If not, I would suggest posting > some example code that fails, because calling truncate() seems to be the > correct approach here. > > -- > Paul Johnson - p...@pjcj.net > http://www.pjcj.net >
Re: Truncate Last few lines
Hi Ambuli, a few comments on your code: On Friday 20 May 2011 14:37:52 Ambuli wrote: > Here i paste a perl script to delete last Two Lines. If you want > delete more lines in a file you can specify it. > > Always start with "use strict;" and "use warnings". > use File::ReadBackwards; Include some empty lines between logical paragraphs of your code. > my $filename = 'test.txt'; This is better specified as a command line argument > my $Lines_to_truncate = 2; # Here the line to truncate is mean Remove > only Last Two Lines The style of your variable name is very strange. It should be: [code] my $num_lines_to_truncate = 2; [/code] Also consider specifying it using Getopt::Long. > my $bw = File::ReadBackwards->new( $filename ) > or die "Could not read backwards in [$filename]: $!"; The "or die" should be indented. > my $lines_from_end = 0; > until( $bw->eof or $lines_from_end == $Lines_to_truncate ) > { > print "Got: ", $bw->readline; > $lines_from_end++; > } The print here is redundant and only adds noise and clutter to the output. > truncate( $filename, $bw->tell ); It would be safer to keep track of the position, then destroy $bw, and only then truncate the file. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Understand what Open Source is - http://shlom.in/oss-fs Beliefs are what divide people. Doubt unites them. -- http://en.wikiquote.org/wiki/Peter_Ustinov Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Truncate Last few lines
On May 20, 4:37 am, cmksw...@gmail.com (Ambuli) wrote: > Here i paste a perl script to delete last Two Lines. If you want > delete more lines in a file you can specify it. > > use File::ReadBackwards; > my $filename = 'test.txt'; > my $Lines_to_truncate = 2; # Here the line to truncate is mean Remove > only Last Two Lines > my $bw = File::ReadBackwards->new( $filename ) > or die "Could not read backwards in [$filename]: $!"; > my $lines_from_end = 0; > until( $bw->eof or $lines_from_end == $Lines_to_truncate ) > { > print "Got: ", $bw->readline; > $lines_from_end++; > } > truncate( $filename, $bw->tell ); Although tie'ing is slow, the core module Tie::File provides an easier way: use Tie::File; tie my @array, 'Tie::File', '/path/to/somefile' or die ...; $#array -= 2;# chop two records off the end -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: How to truncate few bytes of file
> "ab" == a b writes: ab> I need to truncate last few bytes of file. these files are big in size. ab> One idea is to write needed bytes to another file and delete the original ab> file, but i am dealing with big files :( ab> dont want to use truncate, it just truncating the size, all data ab> is gone that doesn't make any sense. you want to truncate but not truncate?? do you want the size to remain the same but become null bytes? if so that isn't truncating but overwriting. clarify your actual needs so we can help here. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: How to truncate few bytes of file
> "ab" == a b writes: ab> Hey Thanks all! ab> I got it ;) ab> open (FH, "+< $fname")|| die "\nFailed to open file $fname\n"; you don't need to open the file at all. truncate can take a filename. ab> my $tmp=$fsize-$trunccount; where do those get set? don't name vars $tmp as that tells the reader nothing about the variable's use. ab> seek(FH,$tmp,0); ab> $addr = tell(FH) ; you don't need the seek and tell calls. you have the size you want and you can pass that directly to truncate. ab> truncate(FH, $addr)|| die "\nFailed to truncate $file: $!"; ab> close(fd); ab> print "\nTruncate successful\n"; this can all be reduced to this: my $size = -s $file ; truncate( $file, $size - $truncount ) || die "\nFailed to truncate $file: $!"; uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
putting file columns into arrays
Dear All, I would like a subroutine that will allow me to easily put columns of a tab delimited file into their own arrays. I've been calling the following repeatedly for each column: my @array1 = getcolvals($filehandle, 0); my @array2 = getcolvals($filehandle, 1); ...etc. sub getcolvals { @_ and not @_ % 2 or die "Incorrect number of arguments to getcolvals!\n"; my $myfile = shift; my $mycol = shift; my @column = (); while (<$myfile>) { my ($field) = (split /\s/, $_)[$mycol]; push @column, $field; } return @column; } This accomplishes exactly what I want, but it requires going through the whole file for each column extraction which seems inefficient. Also, I want to know if I can modify the subroutine to return all the (arbitrary number of) columns at once into arrays. Any suggestions? Many thanks, Eric -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: putting file columns into arrays
> "EM" == Eric Mooshagian writes: EM> I would like a subroutine that will allow me to easily put columns EM> of a tab delimited file into their own arrays. EM> I've been calling the following repeatedly for each column: EM> my @array1 = getcolvals($filehandle, 0); EM> my @array2 = getcolvals($filehandle, 1); ...etc. whenever you think you need to name things with numeric parts, you usually need an array. since you want arrays, then you really want an array of arrays. EM> sub getcolvals { EM> @_ and not @_ % 2 or die "Incorrect number of arguments to getcolvals!\n"; that is sort of clunky. why not just check @_ == 2? @_ == 2 or die ... EM> my $myfile = shift; EM> my $mycol = shift; it is usually better to assign from @_. i posted not to long ago several reasons why. check the archives for it. my( $myfile, $mycol ) = @_ ; and in this case you won't need a $mycol since the code will load all the columns into arrays. EM> my @column = (); you don't need to initialize my arrays to () as my does that for you. EM> while (<$myfile>) { this will fail unless you reopen the file each time you call the sub or you seek to the beginning of the file. EM> my ($field) = (split /\s/, $_)[$mycol]; since you are slicing the split and getting one value, you don't need the () around $field. EM> push @column, $field; and you can combing both of those lines into one: push @column, (split /\s/, $_)[$mycol] ; EM> } EM> return @column; EM> } this is untested: # this is a faster and easier way to get lines from a file use File::Slurp ; sub load_columns { my( $file_name ) = @_ ; $file_name or die 'load_columns: missing file name' ; my @lines = read_file $file_name ; my $matrix ; foreach my $line ( @lines ) { my @fields = split ' ', $line ; for my $i ( 0 .. $#fields ) { # build up the array of arrays here. each array gets the next field value push( @{$matrix[$i]}, $field[$i] ) ; } } return $matrix ; } for more on references and perl data structures read: perlreftut perllol perldsc uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/