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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hex 0D or 0D0A line endings

Status
Not open for further replies.

hr

Technical User
Nov 11, 2000
29
CH
Have to resolve a problem within a text file

Inputfile:
1 line: Some text some 1234 \x0D\x0A
2 line: Some text \x0D
3 line: some 1234 \x0D\x0A (part of 2 line)

Reformatted File:
1 line: Some text some 1234 \x0D\x0A
2 line: Some text some 1234 \x0D\x0A

does somebody know to fix this?

I am working on a windows box with perl v5.6.0 from ActiveState
 
Just a thought - if you want to run a script to reformat the input file, then you might make the script do this:

my $split_line = 0;
while(<INPUT_FILE>) {
if ($split_line) {
$split_line = 0;
print OUTPUT_FILE $split_line_part1 . $_;
next;
}
if (/\x0D$/) {
$split_line = 1;
$split_line_part1 = $_;
next;
}
print OUTPUT_FILE $_;
}

Beware that this is completely untested, but I think with a little playing around with it, it should work.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Depending on the size of the file, you could also gulp the whole thing in at once, replace all \x0D\x0A with some character not likely to appear (i.e. /x002), then replace all \x0D with nothing, then replace \x002 with \x0D\x0A.

Alternatively, you could set the record separator to \x0D\x0A, read in the input file, replace \x0D with null, and rewrite it. Here's a slightly modified version of a subroutine I use that ought to do the trick:
Code:
sub CleanInputFile {

my($file) = @_;

my $txtfile = &quot;/yourpath/$file.txt&quot;;
my $datfile = &quot;/yourpath/$file.dat&quot;;

print &quot;   Reading input file\n&quot;;

my $oldrs = $/;
$/ = &quot;\x0d\x0a&quot;;

open TXTFILE, &quot;$txtfile&quot; || die(&quot;Can't open input file $txtfile for reading&quot;);
my @txtfile = <TXTFILE>;
close TXTFILE;

$/ = $oldrs;

print &quot;   Cleaning up input file\n&quot;;
map { s/\x0a//g; } @txtfile;

print &quot;   Writing output file\n&quot;;
open DATFILE, &quot;>$datfile&quot; || die(&quot;Can't open output file $datfile for writing&quot;);
print DATFILE @txtfile;
close DATFILE;

return 1;

} # CleanInputFile
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
#There's always more than one way to do things in PERL. ^_^
#Love it!

#this part makes the test file. Notice the binmode() so \x0d will print by itself on a Win32 box
open (X, &quot;>try.txt&quot;);
binmode(X);
print X &quot;1 asdf&quot; . &quot;\x0d\x0a&quot;;
print X &quot;2 ghjk&quot; . &quot;\x0d&quot;;
print X &quot;3 zxcv&quot; . &quot;\x0d\x0a&quot;;
close(X);

#this here reads in the entire file. I don't like while <file> or the $_ variable
open (Y, &quot;try.txt&quot;);
@all = <Y>;
close(Y);

open (Z, &quot;>try1.txt&quot;);
foreach $line (@all){
#I think this regex is the trick. It looks for \x0d and whatever follows it, and replaces them with whatever follows the \x0d only if what follows it is not a \x0a. Whew!
$line =~ s/[\x0d]([^\x0a])/\1/g;
print Z $line;
print $line;
}
close(Z);
#sleep lets me click on the .pl file from the desktop and it stays there 5 seconds before closing.
sleep(5);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top