-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsed_fix_spaces_tabs.sed
96 lines (75 loc) · 1.93 KB
/
sed_fix_spaces_tabs.sed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# ./spaces.sed [file-path] [-f | --fix] [-h | --help]
function usage() {
echo "USAGE: $0 [file-path] [-f | --fix] [-h | --help]"
exit 1
}
FIX=0 #0-do not perform fix 1-perform fix
if [ $# -eq 0 ]; then
usage
fi
while [ $# -gt 0 ]
do
case "$1" in
-f|--fix )
FIX=1
shift
;;
-h|--help )
usage
shift
;;
* )
if [ -f "$1" ]; then
FILE="$1"
shift
else
usage
fi
;;
esac
done
if [ $FIX -eq 1 ] && [ -f "$FILE" ]; then
echo "Fixing spaces and tabs at the beginning and at the end of lines"
sed -i 's/[[:blank:]]\+$//' "$FILE"
sed -i 's/^[[:blank:]]\+//' "$FILE"
fi
#display graphically space errors
if [ -f "$FILE" ]; then
LINES=0
REGEX_START="^[[:blank:]]+"
REGEX_END="[[:blank:]]+$"
while IFS= read -r line
do
let LINES++
#if there is no space issue on a line, just print the line
echo "$line" | sed -e '/[[:blank:]]\+$/q9' -e '/^[[:blank:]]\+/q7' >/dev/null
if [ $? -eq 0 ]; then
printf %4s "$LINES:" >> temp.txt
echo "$line" >> temp.txt
continue
fi
#print line number
printf %4s "$LINES:" >> temp.txt
#print on the same line spaces/tabs as a red background at the beginning of line
if [[ "$line" =~ $REGEX_START ]]; then
MATCH=`echo "$BASH_REMATCH" | sed 's/\t/|___TAB___|/g'`
echo -e -n "\e[41m$MATCH\e[49m" >> temp.txt
fi
#print on the same line part of line which is correct - which doesnt have spaces/tabs at the beginning
#and at the end of line
echo -e -n "$line" | sed -e 's/^[[:blank:]]\+//' -e 's/[[:blank:]]\+$//' >>temp.txt
#print on the same line spaces/tabs as a red background at the end of line
if [[ "$line" =~ $REGEX_END ]]; then
MATCH=`echo "$BASH_REMATCH" | sed 's/\t/|___TAB___|/g'`
echo -e "\e[41m$MATCH\e[49m" >> temp.txt
else
echo >> temp.txt
fi
done < "$FILE"
cat temp.txt
rm temp.txt
fi
if [ $FIX -eq 1 ]; then
echo
echo -e "\e[42mDONE\e[49m"
fi