Home > Linux > Several way to convert CRLF to LF in linux box

Several way to convert CRLF to LF in linux box

January 19th, 2009

I’m confused by different endline charactors for different systems recently. I organized some information here.

LF(n) — new line
CR(r) — enter
For windows system, it used CRLF(rn) at the end of each line.
For Mac, it used CR(r) at the end of each line.
For Linux, it used LF(n) at the end of each line.

So if you open a windows file in linux or mac system, there is a character ‘^M’ and the end of each line. On the contrary, you will see there is no line break if you open a mac/linux file in windows system.

I used Linux, so I just collect the way to convert the file from CRLF to LF.

1. Used vim

vim somefile
:set ff=unix
:wq

2. used commond tr
tr can delete characters from standard input, writing to standard output. See detail, please man tr

tr -d 'r' < "inputfile" > "outputfile"

3. If you use archlinux, install hd2u package, it includes a tool named dos2unix
With dos2unix, you can convert the file from mac, windows, unix freely.

dos2unix --d2u file

Linux

  1. No comments yet.
  1. No trackbacks yet.
FireStats icon Powered by FireStats