Monday, December 05, 2011

Removing Carriage Return Line Feeds (CRLF's) From Text Files in Unix

In unix, each line in a text file ends with a line feed. Windows text files, however, end each line with a carriage return and a line feed.

Normally, when you ftp a text file between Windows and unix, the end of line characters get converted. Sometimes, however, the carriage return and line feed get translated into Unix. This can happen, for example, if the FTP is set to binary mode before the file is sent.

When a unix file has both a carriage return and a line feed, it will display a control M at the end of each line. You can remove them in the vi editor:

1. vi the file.
2. Type colon to get a search line.
3. Type 1,$s/ctrl v ctrl m/$

Control v is needed to "escape" the entering of control-m.

2 comments:

J. E. Aneiros said...

:se ff=unix
ZZ

All the cr chars are gone!

Praveen Puri said...

Thanks, I didn't know about the :se (or :set) operator. But the ff parameter is not available in all versions of vi. It is not available on the Solaris version.