?
Solved

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

Posted on 1998-07-02
3
Medium Priority
?
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 84

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

764 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