Removing CRLF (0D 0A) from string in Perl

后端 未结 3 740
梦如初夏
梦如初夏 2021-01-18 00:11

I\'ve got a Perl script which consumes an XML file on Linux and occasionally there are CRLF (Hex 0D0A, Dos new lines) in some of the node values which.

The system w

相关标签:
3条回答
  • 2021-01-18 00:55
    $output =~ tr/\x{d}\x{a}//d;
    

    These are both whitespace characters, so if the terminators are always at the end, you can right-trim with

    $output =~ s/\s+\z//;
    
    0 讨论(0)
  • 2021-01-18 01:00

    A few options:
    1. Replace all occurrences of cr/lf with lf: $output =~ s/\r\n/\n/g; #instead of \r\n might want to use \012\015
    2. Remove all trailing whitespace: output =~ s/\s+$//g;
    3. Slurp and split:

    #!/usr/bin/perl -w  
    
    use strict;  
    use LWP::Simple;  
    
       sub main{  
          createfile();  
          outputfile();
       }
    
       main();
    
       sub createfile{
          (my $file = $0)=~ s/\.pl/\.txt/;
    
          open my $fh, ">", $file;
             print $fh "1\n2\r\n3\n4\r\n5";
          close $fh;
       }
    
       sub outputfile{
          (my $filei = $0)=~ s/\.pl/\.txt/;
          (my $fileo = $0)=~ s/\.pl/out\.txt/;
    
          open my $fin, "<", $filei;
             local $/;                                # slurp the file
             my $text = <$fin>;                       # store the text
             my @text = split(/(?:\r\n|\n)/, $text);  # split on dos or unix newlines
          close $fin;
    
          local $" = ", ";                            # change array scalar separator
          open my $fout, ">", $fileo;
             print $fout "@text";                     # should output numbers separated by comma space
          close $fout;
       }
    
    0 讨论(0)
  • 2021-01-18 01:05

    Typical, After battling for about 2 hours, I solved it within 5 minutes of asking the question..

    $output =~ s/[\x0A\x0D]//g; 
    

    Finally got it.

    0 讨论(0)
提交回复
热议问题