EOL: How to convert a DOS-type text file to UNIX and vice versa

Select DOS, Unix text editor menu

With this simple tutorial, we solve a small mishap that surely will have happened to you. Many times it will have happened to us that when opening a .txt file in Linux or Windows, the text is modified from how we had edited it at the beginning. What happens is that the lines have joined us and the spaces do not appear. This is because the UNIX type text file format differs from the DOS type in terms of the character of end-of-line (EOL) that they employ.

For UNIX systems, the character is a "line feed / newline" or LF and that corresponds to \ n, if you program it will sound to you. While in Windows the system used in the old DOS is inherited, that is, a "carriage return" followed by a "line feed / newline" (\ r \ n). If, for example, we have edited the file in Linux and we are opening it with Windows (and the format in which we have saved it is not appropriate), then there will be the happy problem of having all the text pasted without space or line breaks.

In Linux it is easy to convert from one format to another without installing anything on our system, just use these commands:

  • FROM DOS TO UNIX:

We will use the command tr, followed by the -d parameter and the appropriate EOL character. We will also use the famous "pipes" to operate with the DOS text file ("filename") and the one we want to generate converted into UNIX format ("filename"). Of course we must first use the cd command to go to the folder where the file to be converted is located or from the graphical interface pass the file to / Home (by default where the prompt is located):

tr -d '\n' < nombre_archivo > nombre_fichero</p>
  • FROM UNIX TO TWO:

Now we will use the command thirst to transform a Unix file into a DOS type. For this we will use the following syntax, substituting the name of the files for the file to be converted. I want to make a clarification, whenever I write file I mean Unix type and when I put file to Windows / DOS type.

sed 's/$/\r/' nombre_fichero > nombre_archivo</p>

Anyway, in modern text editors, it already gives us the option of "Save as"In the format we want without having to do it from the terminal. But it is always good to know more about the Linux command line. Another option is to use two tools designed for this purpose, dos2unix and unix2dos, but in many distributions they are not installed by default and should be installed ...


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   blahblah1233445. said

    The correct statement is
    tr -d 'r' filename