source: other-projects/gti/remove_extra_lines.sh@ 30722

Last change on this file since 30722 was 30722, checked in by ak19, 6 years ago

Remove repeated empty lines, leaving just a single blank line between lines that were separated by many more blank lines.

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#! /bin/bash
2
3cp $1 $1.bak
4
5CLEANVERSION_NAME="$1.clean"
6
7# http://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-bash-script
8#sed $'s/\r$//' $1 > $CLEANVERSION_NAME
9#mv $CLEANVERSION_NAME $1
10
11# echo $1
12
13# Removing lines that will become completely empty after sed operation
14# http://soft.zoneo.net/Linux/remove_empty_lines.php
15# http://www.grymoire.com/Unix/Sed.html#uh-30
16sed '/ *# -- Missing translation/d' $1 > $CLEANVERSION_NAME
17mv $CLEANVERSION_NAME $1
18
19# Now we got rid of all the empty lines created by removing "Missing translation" lines
20# We remove any lines containing "Missing translation" but also containing other stuff
21
22cat $1 | sed -e 's/# -- Missing translation:.*$//' > $CLEANVERSION_NAME
23
24# Remove comment of form: #Updated 21-Nov-2011 by <name>
25# The date can be either 1 or 2 digits. It need not be followed by "by <name>"
26#cat $CLEANVERSION_NAME | sed -e 's/# Updated \d\d-[a-zA-Z]{3}-\d{4} by.*$//' > $1
27#cat $CLEANVERSION_NAME | sed -e 's/ *# Updated .* (by.*)?$//' > $1
28cat $CLEANVERSION_NAME | sed -e 's/ *# Updated .[0-9]*\-...\-.....*$//' > $1
29
30# To test if there were ultimately no relevant changes, work on the temporary *.clean file:
31# remove all lines that are merely empty/spaces
32# and any lines that are purely comments (though these may start with spaces)
33# Then test if the *.clean file is non-empty at the end of it
34# double sed operation to remove all comments and then all empty lines:
35# http://stackoverflow.com/questions/22080937/bash-skip-blank-lines-when-iterating-through-file-line-by-line
36#cat $1 | sed -e 's/^\s*(#.*)$//' > $CLEANVERSION_NAME
37sed -e '/\s*#.*$/d' -e '/^\s*$/d' $1 > $CLEANVERSION_NAME
38
39
40# if the *.clean file is actually empty, print a message
41# http://www.unix.com/shell-programming-and-scripting/21753-check-if-file-empty.html
42if [[ ! -s $CLEANVERSION_NAME ]] ; then
43 echo "@@@ $1 only has comments and empty lines/spaces."
44else
45
46
47 # remove empty newlines, modifying the file in place.
48 # http://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed
49 # http://unix.stackexchange.com/questions/76061/can-sed-remove-double-newline-characters
50 #sed -i '/^$/d' $1
51
52
53 # remove empty spaces from lines that are purely empty spaces, but leave
54 # the newline intact, as we'll remove it further below.
55 sed -e 's/^\s*$//' $1 > $1.tmp
56
57 # If there's multiple new lines (3 or more) make them just 2. So leave
58 # one blank line between lines wherever there were many more blank lines
59 # http://stackoverflow.com/questions/922449/how-can-i-replace-mutliple-empty-lines-with-a-single-empty-line-in-bash
60 # "cat with the -s option causes it to remove repeated empty lines from its output"
61 # Beware: on Solaris, the -s option to cat can mean "cat is silent about missing files"
62 cat -s $1.tmp > $1
63fi ;
64rm $CLEANVERSION_NAME
65
66
67echo "**** cleaned version $1"
68echo "**** pre-cleaned original is in $1.bak"
Note: See TracBrowser for help on using the repository browser.