Remove CR from CRLF

GIT does not accept text files in DOS style with CRLF characters at the end of the lines. Although it’s possible to configure repository to auto-convert line endings, the files in your local copy will remain in DOS-style. The script below, assuming it’s saved as fromdos.sh, will prepare your files by removing CR character.

It can be triggered in two ways:

conversion of a single file

1fromdos.sh style.css

conversion of multiple files

1ls *.php | xargs fromdos.sh

Copy the test below and save it as fromdos.sh file in a directory in your PATH. Make sure it’s executable

1chmod +x fromdos.sh

and run it from any place

1#!/bin/sh
2 
3# remove CR from the given file(s)
4die () {
5 echo >&2 "$@"
6 exit 1
7}
8if [ -z "$1" ]
9then
10 die "Usage: `basename $0` file_to_convert \n You can also xargs multiple file by: ls *.php | xargs `basename $0`"
11fi
12 
13for toconv in "$@"
14do
15 cat $toconv | tr -d '\015' > $toconv.tmp
16 rm $toconv
17 mv $toconv.tmp $toconv
18done

Leave a Reply

Your email address will not be published.

Please, enter correct number below to prevent spam (required)