-
Notifications
You must be signed in to change notification settings - Fork 3
/
compress.awk
41 lines (34 loc) · 921 Bytes
/
compress.awk
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
# Helper functions
function trim(s) {
gsub(/^[ \t\r\n]+|[ \t\r\n]+$/, "", s);
return s;
}
function printRecordAndCount(record, count) {
print record;
if (count > 1) {
printf("... repeated %d times\n", count)
}
}
BEGIN {
# Before processing, set the record separator to the ASCII record separator character \x1e
RS = "\x1e";
}
# This action is executed for each record
{
# Build our current var from the trimmed record
current = trim($0);
# Bump the count of times we have seen it
seen[current]++;
# Print the previous record and its count (if it is not identical to the current record)
if (previous && previous != current) {
printRecordAndCount(previous, seen[previous]);
}
# Store the current record as the previous record
previous = current;
}
END {
# After processing, print the last record and count if it is non-empty
if (previous) {
printRecordAndCount(previous, seen[previous]);
}
}