[HRPM] works but ugly

Scott Cornette cornette at infi.net
Thu May 11 12:33:19 CDT 2000


If you are looking to strip off the ^M characters at the end of each line
you will want to use s/\r//;

I am also including a program that I wrote/modified to strip all these
characters from files that are transferred to Unix from DOS/Windows based
machines as binary files accidentally.

Here it is:

#!/usr/local/bin/perl

# dos2unix.pl by David Efflandt <efflandt at xnet.com>
# Modification of script from "Learning perl" p.353
# O'Reilly & Associates, Inc.
#
# Run after transfering text files from DOS to UNIX system.
# Strips carriage returns from DOS files for use UNIX.
# Transfers file permissions to new file (except suid bit).
#
#       Usage:\tdos2unix.pl FILELIST
#       where FILELIST = one or more filenames
#
# If you edit this file in DOS you can run it on itself by typing:
#       perl dos2unix.pl dos2unix.pl
#
# Modify variables below for other search and replace functions.

$find = "\r";   # find this
$sub = undef;   # substitute with this
$rm_bak = 1;    # remove old file after conversion: 0 = no, 1 = yes

while (<>) {
        if ($ARGV ne $oldargv) {
                ($dev,$ino,$mode,$nlink,$uid,$gid) = stat($ARGV);
                $backup = $ARGV . '.bak';
                rename($ARGV, $backup);
                open (ARGVOUT, ">$ARGV");
                chmod $mode, $ARGV;
                select(ARGVOUT);
                $oldargv = $ARGV;
        }
        s/$find/$sub/;
} continue {
        print;
        if (eof) {
                print STDOUT "Converted: $oldargv\n";
                unlink $backup if $rm_bak;
        }
}
select(STDOUT);

Enjoy,

---------------------------
Scott Cornette
Team Leader
InfiNet Product Development
cornette at infi.net
757.624.2689
---------------------------

-----Original Message-----
From: owner-norfolk-pm-list at pm.org
[mailto:owner-norfolk-pm-list at pm.org]On Behalf Of Troy E. Webster
Sent: Thursday, May 11, 2000 12:18 PM
To: norfolk-pm-list at happyfunball.pm.org
Subject: [HRPM] works but ugly


Ok,

I wrote this little script to take care of large blocks of white space and
also to strip off windoze generated carriage-returns/newlines from html
pages.  Trust me, it is usefull to me in certain situations.

Problem is I don't know how a '^M' (or windoze carriage return) is
represented in perl, so I just yank off two characters using chop() and
hope for the best.  Definetly not bullet proof. If anyone knows how I
could better this please let me know. It's my first perl program from
scratch.

thanks
Troy

****************************************************************
#!/usr/local/bin/perl -w
######################################################
# code.pl
#
# This is a simple script which will process html files
# and remove annoying blank lines in an html page's
# source code which are generated by software packages
# such as Cold Fusion. Hand-editing these pages is
# tedious.
#
# ERROR: If a line contains only a single letter and
#        a \n (or a line with only two characters)
#        then this script will chop it out. The
#        assumption is that no html file will exhibit
#        this characteristic.(bad assumption)
#
# NOTE:  This script will work on pages generated by
#        a UNIX platform-specific application OR a
#        page which was created in Windows. (there
#        are different "newlines" associated with each)
#
# AUTHOR: Troy Webster May 2000
######################################################
die "Usage: code.pl [filename]\n" if (@ARGV < 1);

foreach (@ARGV) {
	print "Processing file: $_\n";

	my @lines;
	if(open(FH, "<$_"))
	{
		@lines = <FH>;    #throw it into an array
		close(FH);

		if(open(FH,">$_"))
		{
		    foreach(@lines)
		    {

			if ($_ eq "\n")
			{
			    print "removing blank line.\n";
			    chop $_;
			}

		        elsif (length($_) == 2)  # not the answer,
						 # but it works
			{
			    print "Length is 2\n";

			    chop;
			    chop;

			}

			print FH $_;
		    }
		    close(FH);
		}
		else
		{
		    die "File $_ not written.\n";
		}
	    }
	else
	{
	    die "File $_ not opened.\n";
	}

    }
exit;







___________________________________________________________________________
		- improvise, adapt, overcome -

		www.pcs.cnu.edu/~twebster/
---------------------------------------------------------------------------




More information about the Norfolk-pm mailing list