perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character
tried the below options,

  1. perl -p -i -e 's/^Z//g' new.txt
  2. 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.

If it's the last byte in the file, just use the truncate system call. Perl provides a wrapper for it. truncate - Perldoc Browser.

Regards,
Alister

Can anyone give the entire code(using truncate) to achieve this please.
If any other solution also please let me know thanks.

try this

 
perl -i -pe '$_=~s/^Z// if eof' filename

See

perldoc -f truncate

Be careful, it may destroy your file:

dd bs=filesize-1 seek=1 if=/dev/null of=file
#!/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';
}