[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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
0
phantros
Asked:
phantros
1 Solution
 
RobWMartinCommented:
Instead of chomp($target), try $target=~s/\r//
0
 
RobWMartinCommented:
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).

0
 
maneshrCommented:
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";
=================================================
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
Kim RyanIT ConsultantCommented:
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.
0
 
monasCommented:
phantros,

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

0
 
monasCommented:
There were cases when this had help :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now