Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing extra character at end of file

Status
Not open for further replies.

lgarripee

Programmer
Apr 21, 2009
1
US
My perl skills are self taught so please be gentle. I'm trying to remove the hex character(0C) from the end of a file. My initial strategy was to loop through each line and use regex to search and replace. I think my problem is that the when the last line is read it only includes the string to the LF and ignoring the hex0C that follows. Here's a portion of the code:

Code:
my $hdr_file_tmp = $hdr_file.".tmp";
open(HDRFILE, "$file{'raw_gz'}"."$hdr_file") || die "could not open $hdr_file! $!";
open(HDRFILETMP,">>$file{'raw_gz'}"."$hdr_file_tmp") || die "could not open $hdr_file_tmp! $!";
while (my $iline=<HDRFILE>)
{
	$iline =~ s/\x0C//g;
	print HDRFILETMP $iline;
}
close(HDRFILE) || die "could not close $hdr_file! $!";
close(HDRFILETMP) || die "could not close $hdr_file_tmp! $!";
unlink("$file{'raw_gz'}"."$hdr_file") || die "could not remove $hdr_file! $!";
rename("$file{'raw_gz'}"."$hdr_file_tmp", "$file{'raw_gz'}"."$hdr_file") || die "could not rename $hdr_file_tmp! $!";

This has to be a simple problem. Any ideas?

Thanks in advance.
 
Try using [tt]binmode HDRFILE;[/tt] after opening. However can't see why you check the whole file for a character that may only be at the end.
Note also that it is not necessary to [tt]unlink[/tt] before the [tt]rename[/tt] : perl will overwrite any existing file with [tt]rename[/tt].

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Replace this part:

while (my $iline=<HDRFILE>)
{
$iline =~ s/\x0C//g;
print HDRFILETMP $iline;
}

by this.
[tt]
binmode HDRFILE;
my $octet;
while (<HDRFILE>) {
$octet.=$_;
}
$octet=~s/\x0c$//;
binmode HDRFILETMP;
print HDRFILETMP $octet;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top