Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for crlf2lf.pl

(view source code of crlf2lf.pl as plain text)

  1. #! perl
  2.  
  3. # Store on screen help text in a variable
  4. $syntax = "\nCrLf.pl, Version 1.00\n";
  5. $syntax = $syntax."Replace CR/LF pairs (0D0Ax) with linefeeds only (0Ax)\n\n";
  6. $syntax = $syntax."Usage:  CRLF2LF.PL [ infile [ outfile ] ]\n";
  7. $syntax = $syntax."or:     any_command | PERL.EXE CRLF2LF.PL > outfile\n";
  8. $syntax = $syntax."or:     CRLF2LF.PL /?\n\n";
  9. $syntax = $syntax."Where:  \"any_command\"  is a command that's standard output is used\n";
  10. $syntax = $syntax."                       instead of \"infile\"\n";
  11. $syntax = $syntax."        \"infile\"       is an ASCII file with improper line terminations\n";
  12. $syntax = $syntax."                       (default: standard input)\n";
  13. $syntax = $syntax."        \"outfile\"      is the corrected ASCII file\n";
  14. $syntax = $syntax."                       (default: standard output)\n\n";
  15. $syntax = $syntax."Written by Rob van der Woude\n";
  16. $syntax = $syntax."http://www.robvanderwoude.com\n";
  17.  
  18. # Help required?
  19. if ( @ARGV[0] eq "/?" ) {
  20. 	die $syntax;
  21. }
  22.  
  23. # Use source file as standard input if specified
  24. if ( @ARGV[0] ) {
  25. 	open( STDIN, "< @ARGV[0]" ) || die "Cannot open file @ARGV[0]:\n$!\n\n$syntax";
  26. }
  27.  
  28. # Use target file as standard output if specified
  29. if ( @ARGV[1] ) {
  30. 	open( STDOUT, "> @ARGV[1]" ) || die "Cannot open file @ARGV[1]:\n$!\n\n$syntax";
  31. }
  32.  
  33. # Switch to binary mode
  34. binmode( STDIN  );
  35. binmode( STDOUT );
  36.  
  37. # Search and replace standard input and write to standard output
  38. while ( <STDIN> ) {
  39. 	$line = $_;
  40. 	$line =~ s/\015\012/\012/g;
  41. 	print $line;
  42. }
  43.  
  44. # Close "file" handles
  45. close( STDIN  );
  46. close( STDOUT );
  47.  

page last modified: 2024-04-16; loaded in 0.0233 seconds