Link to home
Start Free TrialLog in
Avatar of phantros
phantros

asked on

newbie perl editing question

I am trying to edit and upload some perl scripts I found on the net, from my win98 machine to a unix one.  If I edit them in notepad they work fine.  If I use wordpad or word, they don't; I think some newline characters are being tacked in? I have one script that is too large to use with notepad.  I tried to make a script that would strip out the newlines, and the file size did decrease, but it still won't work.  Here's the script I was trying to "chomp" with:

#!/usr/bin/perl
print "Chomp up what file: ";
$filename=<STDIN>;
chomp $filename;
open(FILE,$filename);
$/="\r\n";
while(defined($target=<FILE>)) {
  chomp($target);
  $stuff.=$target;
}
close(FILE);
open(NEW,">new.cgi");
print NEW $stuff;
close(NEW);
print "Chomped $filename";

I'm looking for one of three answers:
a. Tell me how to edit a large file without putting newlines in,
b. tell me how to make some type of script like the one above to strip them out after, or
c. tell me I'm totally offbase, and what the real reason is this darn thing isn't working. 8-)
(I'm a total perl/unix newbie)
Thanks
ASKER CERTIFIED SOLUTION
Avatar of RobWMartin
RobWMartin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobWMartin
RobWMartin

Also, leave out the $/="\r\n".  The chomp is taking out both the \r and the \n.  Or you could leave in the $/="\r\n" and the chomp($target), then change to $stuff.="$target\n" (i.e. add the \n).

if i understand you right, you get some strange character in your script when you transfer your file from Win98 to Unix, right??


If yes, you might be seeing ^M chars.

This is due to the fact, as you might already know, that DOS/Win* use 2 chars to denote end of line char where as UNIX used only one.  Therefore the extra char is not because of something you have/have not done.

b. tell me how to make some type of script like the one above to strip them out after, or


there is more than one way of doing this.

I) IF you are using the vi editor, in UNIX to edit that file, you can use the foll ..

Lets say the script name is test.pl. At the prompt type..

1 - vi test.pl and press the enter key
2 - press escape key. press Shift + ; and type the foll

1,$s/^M//g

yes this is very similar to the PERL search and replace pattern.

what you are saying is from line no 1 to end of file search for the ^M char and remove it globally. NOTE: to reproduce the ^M char , keep the CTRL key pressed and press v and them m (so you get a SINGLE char ^M)

II) If you want to use a PERL script, change your script to ......

#!/usr/bin/perl

print "Chomp up what file: ";
$filename=<STDIN>;
chomp $filename;
open(FILE,$filename);


##$/="\r\n";

$/="^M";
## To reproduce the control char, keep the ctrl key pressed and press v followed by m.

while(defined($target=<FILE>)) {
chomp($target);
$stuff.=$target;
}
close(FILE);
open(NEW,">new.cgi");
print NEW $stuff;
close(NEW);
print "Chomped $filename";
=================================================
You could try some shareware programmers editors like TextPad or EditPlus. They allow you to choose either Unix or Windows style line terminators, and will edit large files. This should be easier than having to run conversion scripts over your programs all the time.
phantros,

      if the only problem uploaded scripts have \r's at the end of line - upload your scripts via ftp in TEXT (!!!!! :-) mode!
Hold on there, monas.  You are making things look too easy :^)

There were cases when this had help :-)