harsha.reddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I was trying to split the contents of a tsv file.

open TSV "./MyTsv.txt" or die("Gone") while(<TSV>) { print; } close(<TSV>);
(My TSV file contains values atoms which has \n (LF) character in them, and each row ends with CRLF.)

I see that in the code the lines are (split) or printed based on \n (LF) and not based on \r\n (CRLF).

I wanted to split the lines based on CRLF;

how do I tell my Perl script to consider that a CRLF and end of line instead of LF as end of line?

help me to figure this out, please.

Cheers!!
Harsha Reddy

Replies are listed 'Best First'.
Re: Spliting a TSV file on the basis of CRLF
by quester (Vicar) on Jan 02, 2008 at 07:51 UTC
    You can set the line delimiter to CRLF like this:
    $/ = "\r\n";
    or more readably like this:
    use English '-no_match_vars'; $INPUT_RECORD_SEPARATOR = "\r\n";
    You can also use $RS as a synonym for $/. See perlvar for the details.
      This is yummy!! Thanks a lot :)
        oops, My effort went in vain :(
        
        use strict; use warnings; use English '-no_match_vars'; local($INPUT_RECORD_SEPARATOR) = "\r\n"; open my $fh, "./MyTsv.txt" or die $!; while(<$fh>) { print;}
        
        prints this:
        
        define  HOSTNAME        indpdsol3
        
        define  DBPATH  /ccmdb/basedb
        
        TestCaseID      Prefix  Command Postfix Wait    MatchString     MatchOnly
        LineCount       StatusCode
        TC001   cd ~/   ccm start       cd ~/ccm_wa     0       "This
        this
        and
        this"   1       4       0
        _LINE_BREAK_MARKER_
        
        
        
        When I open the same file in a editor I see CRLF like this:
        
        
        
        
        define  HOSTNAME        indpdsol3 <CRLF>
        define  DBPATH  /ccmdb/basedb <CRLF>
        <CRLF>
        TestCaseID      Prefix  Command Postfix Wait    MatchString     MatchOnly
        LineCount       StatusCode<CRLF>
        TC001   cd ~/   ccm start       cd ~/ccm_wa     0       "This<LF>
        this<LF>
        and<LF>
        this"   1       4       0<CRLF>