#! /bin/bash cp $1 $1.bak CLEANVERSION_NAME="$1.clean" # http://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-bash-script #sed $'s/\r$//' $1 > $CLEANVERSION_NAME #mv $CLEANVERSION_NAME $1 # echo $1 # Removing lines that will become completely empty after sed operation # http://soft.zoneo.net/Linux/remove_empty_lines.php # http://www.grymoire.com/Unix/Sed.html#uh-30 sed '/ *# -- Missing translation/d' $1 > $CLEANVERSION_NAME mv $CLEANVERSION_NAME $1 # Now we got rid of all the empty lines created by removing "Missing translation" lines # We remove any lines containing "Missing translation" but also containing other stuff cat $1 | sed -e 's/# -- Missing translation:.*$//' > $CLEANVERSION_NAME # Remove comment of form: #Updated 21-Nov-2011 by # The date can be either 1 or 2 digits. It need not be followed by "by " #cat $CLEANVERSION_NAME | sed -e 's/# Updated \d\d-[a-zA-Z]{3}-\d{4} by.*$//' > $1 #cat $CLEANVERSION_NAME | sed -e 's/ *# Updated .* (by.*)?$//' > $1 cat $CLEANVERSION_NAME | sed -e 's/ *# Updated .[0-9]*\-...\-.....*$//' > $1 # To test if there were ultimately no relevant changes, work on the temporary *.clean file: # remove all lines that are merely empty/spaces # and any lines that are purely comments (though these may start with spaces) # Then test if the *.clean file is non-empty at the end of it # double sed operation to remove all comments and then all empty lines: # http://stackoverflow.com/questions/22080937/bash-skip-blank-lines-when-iterating-through-file-line-by-line #cat $1 | sed -e 's/^\s*(#.*)$//' > $CLEANVERSION_NAME sed -e '/\s*#.*$/d' -e '/^\s*$/d' $1 > $CLEANVERSION_NAME # if the *.clean file is actually empty, print a message # http://www.unix.com/shell-programming-and-scripting/21753-check-if-file-empty.html if [[ ! -s $CLEANVERSION_NAME ]] ; then echo "@@@ $1 only has comments and empty lines/spaces." else # remove empty newlines, modifying the file in place. # http://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed # http://unix.stackexchange.com/questions/76061/can-sed-remove-double-newline-characters #sed -i '/^$/d' $1 # remove empty spaces from lines that are purely empty spaces, but leave # the newline intact, as we'll remove it further below. sed -e 's/^\s*$//' $1 > $1.tmp # If there's multiple new lines (3 or more) make them just 2. So leave # one blank line between lines wherever there were many more blank lines # http://stackoverflow.com/questions/922449/how-can-i-replace-mutliple-empty-lines-with-a-single-empty-line-in-bash # "cat with the -s option causes it to remove repeated empty lines from its output" # Beware: on Solaris, the -s option to cat can mean "cat is silent about missing files" cat -s $1.tmp > $1 fi ; rm $CLEANVERSION_NAME echo "**** cleaned version $1" echo "**** pre-cleaned original is in $1.bak"