I need to determine the kind of line ending (Windows, Unix of Mac) used in each of a set of input files and based on the kind of line endings convert the line endings of the file to Unix.

Assume that the array @Files holds the paths to the files of interest

pseudocode would be:

For each file $F in @Files

if $F is a text file:

my LE = getEndings($F) #$LE is the endings type, Windows, Unix or Mac

if ($LE eq 'Windows' or $LE eq 'Mac') {change $F to Unix line endings}

getEndings is a subroutine that determines the line endings

The files of interest can be huge so I can't expect to slurp the entire file contents into memory. Instead, to convert a Windows or Mac file I expect to read the file line by line, chomp each line, then writing that line with usual Unix line endings to a file named temp.txt. After closing temp.txt I would remove the original file and rename temp.text to the original file name.

I have not been able to figure out how to write a getEndings subroutine that works with all three kinds of line endings. Any suggestion will be much appreciated<\p>

The notion to read files line by line and write lines to temp.txt should work for files with Windows endings but with Mac line ending the lines would not be read correctly. Any ideas?


In reply to How to determine type of line endings in a text file from within a script by barryghall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.