[Pdx-pm] data munging: line-endings

Thomas Keller kellert at ohsu.edu
Tue Oct 14 13:48:09 CDT 2003


On Tuesday, October 14, 2003, at 11:14  AM, Colin Kuskie wrote:
> s/\s*$//;
This doesn't help for the files I need to process. "$/" needs to be 
correct for correct line-by-line processing.
I've been running them through the following filter, but it requires 
that I know ahead of time that the input file has DOS type 
line-endings. I guess that's not too onerous, but if we change the 
application generating these files, the line-endings may change. But I 
guess I'll see that immediately and be able to change the specification 
to the filter script.
Here's what I use now (From D. Cross "Data Munging with Perl")
# ref: D. Cross, p89.

use lib "/Library/Perl";
use strict;

(@ARGV == 3) or die "Useage: requires arguments-> filename-to-filter 
source-format/
						(e.g. Win) target-format(e.g. Unix or Mac).";

my ($file, $src, $tgt) = @ARGV;

my %conv = (	Mac => "\cM",	## Mac used CR
				Unix => "\cJ",			## Unix uses LF
				Win => "\cM\cJ");		## Windows uses both

$src = $conv{$src};
$tgt = $conv{$tgt};

$/ = $src;


open F, $file or die "Can't open $file: $!";
while (<F>) {
	s/$src/$tgt/go;
	print;
}

close F;




More information about the Pdx-pm-list mailing list