: # Find perl in the PATH eval 'exec perl -S $0 ${1+"$@"}' if 0; # LEG - Line Ending Ginsu # Change line endings to either Unix, Mac or DOS style # # These are the assumed line endings for each system: # Unix line endings: '\n' # Mac line endings : '\r' # DOS line endings : '\r\n' # # Mixed files are no problem, if a text file contains both DOS and Mac # line endings and conversion to Unix line endings is desired all # line endings will be converted to be Unix-like. I don't know why # you'd have such a file but it's no problem for the Ginsu(tm). use 5.005; # Need regex lookbehind assertions (Perl >= 5.5) use strict; use Getopt::Long; my $version = 2004.117; my $verbose = 0; my $infile = undef; my $outfile = undef; my $ule = undef; my $mle = undef; my $dle = undef; # Parse command line arguments my $getoptsret = GetOptions ( 'verbose|v' => \$verbose, 'unix|u' => \$ule, 'mac|m' => \$mle, 'dos|d' => \$dle, ); if ( ! $getoptsret || $#ARGV < 0 || $#ARGV > 1 ) { print "Line Ending Ginsu, change text file line endings ($version)\n\n"; print "Usage: $0 [-vumd] inputfile [outputfile]\n\n"; print " v,verbose Report what's going down\n"; print " u,unix Convert to Unix line endings [\\n] (default)\n"; print " m,mac Convert to Mac line endings [\\r]\n"; print " d,dos Convert to DOS line endings [\\r\\n]\n\n"; print "If '-' is given for the input file the input will be "; print "read from stdin\n"; print "If '-' is given for the output file the output will be "; print "sent to stdout.\n"; print "If no output file is specified the input file will "; print "be modified in place\n"; exit (1); } if ( ($ule + $mle + $dle) > 1 ) { die "Cannot specify more than one conversion\n"; } $infile = $ARGV[0]; $outfile = ( $ARGV[1] ne "" ) ? $ARGV[1] : $infile; if ( $verbose ) { print "Inputfile: $infile\n"; my $outf = ( $outfile eq "-" ) ? "STDOUT" : $outfile; print "Output file: $outf\n"; } # Read (slurp) in input as a block open (IN, "<$infile") || die "Cannot open input file: '$infile'\n"; my $inblock = do { local $/, }; close IN; # Keep track of the number of substitutions my $subs = 0; if ( defined $dle ) { # Translate to DOS line endings (\r\n) $subs += $inblock =~ s/\r(?!\n)/\r\n/g; # From Mac line endings $subs += $inblock =~ s/(?&STDOUT") || die "Cannot open STDOUT for output\n"; } else { open (OUT, ">$outfile") || die "Cannot open $outfile for output\n"; } print OUT $inblock; __END__