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 ( ), use the command:

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

Warning

This will delete all 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 for carriage return and 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)



Backspace

f

Form feed

Newline

Return

Tab (sometimes called a “horizontal” tab)

v

Vertical tab

See Also

  • man tr

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.6.114