简体   繁体   中英

How to remove CR LF end of line in Perl

I need to remove and of the line which looks like CR LF.

Coding - Windows-1250 Windows 7 EN

I have been trying to chomp, chomp, change \\R to nothing change \\r\\n etc but nothing works...

Thank you in advance

use strict;
$/ = "\r\n";
open FILE , "<", "file.txt" or die $!;
while (<FILE>) {
    my @line = split /,/ , $_;

    foreach my $l (@line) {
        print $l;
    }
    sleep(1);
}

First of all, you don't even try to change the CRLF to LF. You just print back out what you got.

On a Windows system, Perl adds the :crlf layer to your file handles. That means that CRLF gets changed to LF on read, and LF gets changed to CRLF on write.

That last bit is the problem. By default, Perl assumes you're create a text file, but what you're creating doesn't match the definition of a text file on Windows. As such, you need to switch your output to binmode .

Solution that only works on a Windows system:

use strict;
use warnings;

binmode(STDOUT);

open(my $fh, '<', 'file.txt') or die $!;
print while <$fh>;

Or if you want it to work on any system,

use strict;
use warnings;

binmode(STDOUT);

open(my $fh, '<', 'file.txt') or die $!;
while (<$fh>) { 
   s/\r?\n\z//;
   print "$_\n";
}

Without binmode on the input,

  • You'll get CRLF for CRLF on a non-Windows system.
  • You'll get LF for CRLF on a Windows system.
  • You'll get LF for LF on all systems.

s/\\r?\\n\\z// handles all of those.

如果你在Unix上就像命令行一样,在shell上提示以下做诀窍:

  • perl -pe 's/^M//g' file.txt # ^M mean control-M, press control-v control-M, the CRLF character
  • perl -pe 's#\\r\\n$#\\n#g' file.txt
  • This works for me on a Mac (Mac OS X 10.7.5, Perl 5.16.2):

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    while (<>)
    {
        print "1: [$_]\n";
        {
            local $/ = "\r\n";
            chomp;
        }
        print "2: [$_]\n";
    }
    

    Sample output:

    $  odx x3.txt
    0x0000: 6F 6E 69 6F 6E 0D 0A 73 74 61 74 65 0D 0A 6D 69   onion..state..mi
    0x0010: 73 68 6D 61 73 68 0D 0A                           shmash..
    0x0018:
    $ perl x3.pl < x3.txt | vis -c
    1: [onion^M
    ]
    2: [onion]
    1: [state^M
    ]
    2: [state]
    1: [mishmash^M
    ]
    2: [mishmash]
    $
    

    The odx program gives me a hex dump of the data file; you can see that there are 0D 0A (CRLF) line endings. The vis -c program shows control characters (other than newline and tab) as ^M (for example). You can see that the raw input includes the ^M (lines starting 1: ) but the chomp 'd lines are missing both the newline and the carriage return.

    The only issue will be whether the input on Windows is a text file or a binary file. If it is a text file, the I/O system should do the CRLF mapping automatically. If it is a binary file, it won't. (Unix doesn't have a meaningful distinction between text and binary files.) On Windows, you may need to investigate binmode , as discussed on the open page.

    That would be a one-liner in Perl... Try the following under Linux:

    perl -0pe 's/[\r\n]//g' < file.txt
    sleep 1
    

    and the following under Windows:

    perl.exe -0pe "s/\015\012|\015|\012//g" < file.txt
    ping 1.1.1.1 -n 1 -w 1000 > nul
    

    I think \\s* should work.

    use strict;
    use warnings;
    
    open FILE , "<", "file.txt" or die $!;
    
    while ( my $line = <FILE> ) {
    
        $line =~ s{ \s* \z}{}xms;  # trim trailing whitespace of any kind
    
        my @columns = split /,/ , $line;
    
        for my $column (@columns) {
    
            print "$column ";
        }
        sleep(1);
    
        print "\n";
    }
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM