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

fromdos.sh style.css

conversion of multiple files

ls *.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

chmod +x fromdos.sh

and run it from any place

#!/bin/sh

# remove CR from the given file(s)
die () {
 echo >&2 "$@"
 exit 1
}
if [ -z "$1" ]
then
 die "Usage: `basename $0` file_to_convert \n You can also xargs multiple file by: ls *.php | xargs `basename $0`"
fi

for toconv in "$@"
do
 cat $toconv | tr -d '\015' > $toconv.tmp
 rm $toconv
 mv $toconv.tmp $toconv
done

Leave a Reply

Your email address will not be published.

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