In a 10-50GB file , at end of file there is Control-z character
tried the below options,
perl -p -i -e 's/^Z//g' new.txt
perl -0777lwi -032e0 new.txt
and Sed command, dos2unix etc
it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE DIRECTLY AND REMOVE CTRL-Z CHARACTER . we need tat to be removed in less than a second.
it takes in minutes to remove the control-z in 1GB file.
Please help asap. Thanks in advance.
#!/usr/bin/perl
use strict;
my $abs;
open(FILE ,"tt.txt");
seek(FILE,-2,2);
read(FILE,$abs,1);
print $abs;
$abs=~s/^Z//g;
print FILE "$abs";
print $abs;
close(FILE);
I want to directly go to end of the file (without reading whole big file)
and remove the ctrl-Z character and write back to file.
No need of one-liners(since it works but takes long time for big file)
Update me please
5 minutes of reading documentation would serve you better than days of begging on forums. You've already made 99% of the answer yourself, and are only missing one piece -- the piece we already gave you, truncate().
#!/usr/bin/perl
open(FILE, "tt.txt");
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime,
$ctime, $blksize, $blocks)=stat(FILE);
seek(FILE, $size-1, 0);
read(FILE, $abs, 1);
close(FILE); # Can't truncate while file is open
if($abs =~ /\032/)
{
print "sure enough, tt.txt ends in ^Z\n";
printf("Shrink to %d bytes\n", $size-1);
truncate("tt.txt", $size-1) || die 'could not shrink';
}