Converting DOS Files to Linux Format

Problem

You need to convert DOS formatted text files to the Linux format. In DOS, each line ends with a pair of characters—the return and the newline. In Linux, each line ends with a single newline. So how can you delete that extra DOS character?

Solution

Use the -d option on tr to delete the character(s) in the supplied list. For example, to delete all DOS carriage returns (\r), use the command:

$ tr -d '\r' <file.dos >file.txt

Warning

This will delete all \r characters in the file, not just those at the end of a line. Typical text files rarely have characters like that inline, but it is possible. You may wish to look into the dos2unix and unix2dos programs if you are worried about this.

Discussion

The tr utility has a few special escape sequences that it recognizes, among them \r for carriage return and \n for newline. The other special backslash sequences are listed in Table 8-4.

Table 8-4. The special escape sequences of the tr utility

Sequence

Meaning

\ooo

Character with octal value ooo (1-3 octal digits)

\\

A backslash character (i.e., escapes the backslash itself)

\a

“audible” bell, the ASCII BEL character (since “b” was taken for backspace)

\b

Backspace

\f

Form feed

\n

Newline

\r

Return

\t

Tab (sometimes called a “horizontal” tab)

\v

Vertical tab

See Also

  • man tr

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.