Awk|Bash – Removing invalid characters ^M (Control-M lub carriage return) from file.
Home page Ubuntu C++ Hardware HTML i javascript Java Software SQL

Awk|Bash – Removing invalid characters ^M (Control-M lub carriage return) from file.

Added: 2012-09-08

By downloading the script for Linux, sometimes you might have a problem with its correct running. Message error:

-bash: ./c.bash: /bin/bash^M: bad interpreter: No such file or directory

Information above says, that the file was created, eg with Windows and are were used illegal characters end line \r

In Internet you can find examples how to delete illegal characters from file. Page with examples Konwertowanie plików z Windowsa do Linuksa.

Below is a script that searches all files in a location containing characters \ r More information about the script, type the word --help after it starts.

./isDosFile.bash --help

#!/bin/bash

if [ "$1" == "--help" ]; then
	echo "Script to find files that are on the end of line character \"\\r\"";
	echo "You can run script without parameters.";
	echo "The results are the names of the file with flag:";
	echo "0 - file do not have char";
	echo "1 - file have char";
	echo;
	echo "You can add tow parameters:";
	echo "[optional] - destination folder, but always must be first parameter"
	echo "-r - rewrite file without char \"\\r\"";

	exit 0;
fi;


if [ "$2" == "-r" -o "$1" == "-r" ]; then
	option=1;
else
	option=0;	
fi;

if [ "$1" = ""  -o "$1" = "-r" ]; then
	DEST=`pwd`"/";
else
	if [ ! -d $1 ]; then
		echo "";
		echo "The argument \""$1"\" is not directory!!!";
		echo "";
		exit 1;
	fi;
	DEST=$1"/";
fi;



for file in $DEST*; do 
	if [ -f "$file" ]; then
		awk -vo=$option 'BEGIN{
			flag=0;
		}{
			if (index($0, "\r") != 0){
				flag = 1;
				exit;
			}
		}END{
			if (flag == 1){
				print "1 '"$file"'";
				if (o == 1){
					system("rm -f dos2unix.temp");
					system("tr -d '\\15\\32' < \"'"$file"'\" > dos2unix.temp");
					system("mv dos2unix.temp \"'"$file"'\"");
				}
			}else
				print "0 '"$file"'";

			
		}' "$file";
	fi;
done

echo "End script.";