Solved

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

Posted on 1998-07-02
3
147 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 150 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now