?
Solved

line input doesn't recognize ^M end of line character

Posted on 1998-07-02
3
Medium Priority
?
154 Views
Last Modified: 2010-03-05
I'm working on a Linux computer with perl-5.004-1 and trying to input an ascii data file that was created on a Macintosh computer. The file is
huge and is composed of columns of 3 rows each. The problem is
that the Macintosh newline character (^M) does not seem to be recognized
by Perl as such.  So, when I try to do something like:

         while ($line=<INPUT_FILE>) {
            chop $line;
            @values=split(/ /,$line); #store columns in values array
            print SEGMENTED_FILE "$values[1]\n";
          }

in which I'm attempting to read in the file line by line and output column
1 to SEGMENTED_FILE, it doesn't work (It attempts to read in the entire
file as one line). If it was a Unix data file with a proper newline
character it works fine.

        So, my question is how do I get around this. If I was writing it
in C, then I know that I could read in one field at a time, and just deal
with it that way. In Perl can I do that? Or is there a way to get Perl to
recognize the ^M character as end of line? Any suggestions?

Thanks in advance,
Dave
0
Comment
Question by:djc2
3 Comments
 
LVL 5

Accepted Solution

by:
n0thing earned 600 total points
ID: 1208277
Just remove the ^M, by adding one liner "s/^V^M//;" after the while.
PS: You'll need to press "Ctrl-V, then Ctrl-M" in order for the
^M to come up.

Regards,
Minh Lai
0
 
LVL 85

Expert Comment

by:ozo
ID: 1208278
You could also define
  $/= "\cM";
then your
  while( <INPUT_FILE> ){ chomp; @values=split; ... }
will treat ^M as the end of line

0
 

Author Comment

by:djc2
ID: 1208279
$/= "\cM" worked perfectly!

I didn't try Minh Lai's solution - since my data file is huge, I didn't want to read it all into memory and then process it after the while.

Thanks!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question