fix issues with POSIX grep -on output

This commit is contained in:
Jorge Morante 2016-05-23 07:59:10 +02:00
parent 174cd70339
commit b9f216d966
1 changed files with 16 additions and 1 deletions

View File

@ -31,7 +31,22 @@ do
done < /dev/stdin
IFS=$OLDIFS
matches=$(echo -e $lines | (grep -oniE "$PATTERNS" 2> /dev/null) | sort -u)
# POSIX grep does linenumber on every line with both -o and -n flags set
normalize_grep_output='
BEGIN {
previous_line_no = 0;
}
{
if ( $0 ~ /^[0-9]+:/ ) {
split($0, split_at_colon, ":")
previous_line_no = split_at_colon[0]
print $0
} else {
printf "%d:%s\n", previous_line_no, $0
}
}
'
matches=$(echo -e $lines | (grep -oniE "$PATTERNS" 2> /dev/null) | awk $normalize_grep_output | sort -u)
output="$lines"
i=0