Convert DOS line breaks to Unix line breaks

Contributor Icon Contributed by skreuzer Date Icon October 18, 2003  
Tag Icon Tagged: UNIX

This one liner will convert a file with DOS line breaks to a file with Unix line breaks


tr -d '\15\32' < dos-format-file > unix-friendly-file

Previous recipe | Next recipe |
 
  • .
    Or in a singl e shot
    perl -pi -s 'rn/n/' filename
    dos2unix filename.
  • Doug Merritt
    Bad advice, doesn't work! This says delete (-d)
    all sequences of the form "1532"...that's wrong
    for two reasons.

    First, "32" is a typo, that's just
    a random control character. "12" is the correct
    octal escape for newline (and 15 is carriage
    return).

    This is better expressed as simply "rn" to avoid
    such typos.

    Second, you don't want to DELETE the line
    ending altogether, you want to CHANGE it.

    The quickest fix is to just delete all carriage
    returns, leaving the newlines in place:

    tr -d '15" < dos-format-file > unix-friendly-file
    or better
    tr -d "r" < dos_file > unix_file

    There's also "dos2unix", which comes with most
    Linux/Unix systems I've seen in the last 10 years:

    dos2unix -n dos_file new_unix_file
  • lazy
    sed is made for these kind of things...

    > sed -i "s/r//" file.txt

    If you want to keep a backup of the file just specify an extension.

    > sed -i.bak "s/r//" file.txt

    Above line will create file.txt.bak as a copy of the original file.
  • Anonymous
    <ul id="quote"><h6>Doug Merritt wrote:</h6>Bad advice, doesn't work! This says delete (-d)
    all sequences of the form "1532"...that's wrong
    for two reasons.

    First, "32" is a typo, that's just
    a random control character. "12" is the correct
    octal escape for newline (and 15 is carriage
    return).
    </ul>

    15 is r. Yes, you do want to delete all r's.

    32 is CTRL-Z. DOS text files have an EOF byte: 0x1a or 32 or ^Z. This also removes that character. Yes, you do want to delete all ^Z's.

    So this is good advice and does work. Did you try it?

    I do agree with the other poster who said to use -i to edit the file in place, though.
  • Anonymous
    Running the command:

    tr -d '1532' < teste.f > teste1.f

    Cleans up my ctrl-m's but not the final ctrl-z. Anyone would happen to know why?
blog comments powered by Disqus