LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl's chomp under Win-Linux (https://www.linuxquestions.org/questions/programming-9/perls-chomp-under-win-linux-434508/)

thelonius 04-12-2006 11:01 AM

perl's chomp under Win-Linux
 
Hello,

I have adata file produced under Windows and I treat it with Perl under Linux. The problem I have is that 'chomp' doesn't work in this configuration.

Any idea what to do ?

Thanks.

scuzzman 04-12-2006 12:25 PM

You need to remove the Windows carriage return from the lines as well. You see, when a text file is created under Windows, it ends the lines with a carriage return and a line feed, whereas *nix systems use just a line feed. You can use Perl, tr, sed, or a host of other programs to remove this extra character. Here is a simple Perl script to remove these extra characters and format the text file properly.

thelonius 04-12-2006 12:29 PM

Quote:

Originally Posted by scuzzman
You can use Perl, tr, sed, or a host of other programs to remove this extra character.

...other program like 'dos2unix', but I don't want it. I'd like that Perl takes care about it. But it seams that this is not the case.

Thanks.

scuzzman 04-12-2006 02:13 PM

Then you didn't read my entire post. Try that one again. (Hint: check out the script I linked to)
The specific section I'm referring to is here (better read in-context):
Code:


  while (<OLD>) {
      if ($opt_replace) {
        s/\r/\n/g;          # replace all (/g) \r's by \n's
      } else {
        s/\r//g;          # replace all (/g) \r's by nothing
      }
      print NEW      or die "can't write $new: $!";
  }

This is just a hint of the power of Perl's regular expression implementation. Take a look here.


All times are GMT -5. The time now is 01:27 AM.