Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-08-18 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?