-
Notifications
You must be signed in to change notification settings - Fork 134
/
fix-blanks.sh
executable file
·35 lines (32 loc) · 1.21 KB
/
fix-blanks.sh
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
#!/usr/bin/env bash
import_validate="$(dirname "${BASH_SOURCE[0]}")/import_validate.py"
# The following awk program parses lines (of the form `<file>:<line>:<message`) into
# fields delimited by colons, and groups the second fields (line numbers) keyed by
# the first field (file name). The grouped values are reduced by appending a `d` to
# each line number, and joining all those strings on `;` as the joining character.
# This creates output lines such as `/path/to/some/file.go 2d;7d`, where the second
# token is a sed command for deleting lines 2 and 7.
awk_prog='
{
if ($3 == " Too many blank lines in imports") {
lines[$1] = lines[$1] (lines[$1] == "" ? "" : ";") $2 "d"
} else {
print $0 >"/dev/stderr"
}
} END {
for (a in lines) print a, lines[a]
}
'
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^([^[:space:]]+)[[:space:]]+([^[:space:]]+)$ ]]; then
file="${BASH_REMATCH[1]}"
sed_cmd="${BASH_REMATCH[2]}"
else
echo >&2 "Malformed awk output line $line ..."
exit 1
fi
deletions_only="${sed_cmd//[^d]/}"
echo >&2 "Deleting ${#deletions_only} blank line(s) from $file"
sed -i'.bak' -e "$sed_cmd" "$file"
rm "${file}.bak"
done < <(awk -F: "$awk_prog" <("$import_validate" "$@") )