-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzc
executable file
·42 lines (39 loc) · 2.07 KB
/
lzc
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
#! /bin/sed -f
# lzc - leading zero compression for (hexa)decimal numbers
# Copyright (C) 2018 Erik Auerswald <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Remove leading zeros in (hexa)decimal numbers. For IPv6 addresses this
# is known as "leading zero compression", hence the name. In electrical
# engineering this is known as suppression of leading zeros.
#
# This script removes leading zeros for every hexadecimal integer in the
# input stream, e.g., 0ab.02 -> ab.2 (what looks like a decimal point is
# treated as something different from a number). This allows to use lzc
# on IPv4 and IPv6 addresses embedded in text.
#
# lzc can be used to remove leading zeros from MAC addresses, e.g., to
# generate the MAC address form to grep for in some logs from EXOS.
# Each segment of zeros to remove must be anchored between two non xdigits.
# This means that one s///g invocation skips every second group of xdigits.
# The same s///g invocation can be used twice here.
#s/\(^\|[^[:xdigit:]]\)0\+\([[:xdigit:]]\+\([^[:xdigit:]]\|$\)\)/\1\2/g
#s/\(^\|[^[:xdigit:]]\)0\+\([[:xdigit:]]\+\([^[:xdigit:]]\|$\)\)/\1\2/g
# Alternatively, whenever leading zeros are compressed (or suppressed),
# the script can jump (branch) back to re-start the substitution. With
# this re-starting, the "g" flag to the s/// command is no longer needed.
# This technique is more robust in general.
: restart
s/\(^\|[^[:xdigit:]]\)0\+\([[:xdigit:]]\+\([^[:xdigit:]]\|$\)\)/\1\2/
t restart