[PLUG] Fun sed challenge!

Robert Citek robert.citek at gmail.com
Thu Nov 16 14:05:41 UTC 2017


On Thu, Nov 16, 2017 at 12:09 AM, Russell Senior
<russell at personaltelco.net> wrote:
> Can anyone suggest a nice unix pipeline filter using lightweight tools
> (no python) to output an ipv6 address in reduced format.

Why not python/perl/ruby/etc.?

If only given an IPv6 address, this would work:

echo '2603:01c2:1800:a8c0:0000:0000:0000:0001' |
sed -re 's/:0+/:/g;s/::+/::/g'

Outputs:

2603:1c2:1800:a8c0::1

> For example:
>
> ipv6 Thu Nov 16 00:05:34 PST 2017 [2603:01c2:1800:a8c0:0000:0000:0000:0001] foo bar baz
>
> should become:
>
> ipv6 Thu Nov 16 00:05:34 PST 2017 [2603:1c2:1800:a8c0::1] foo bar baz

A bash solution to preserve date/time stamps:

echo 'ipv6 Thu Nov 16 00:05:34 PST 2017
[2603:01c2:1800:a8c0:0000:0000:0000:0001] foo bar baz' |
tr '][' '\t' |
while IFS=$'\t' read a b c ; do
  b=$(<<< "$b" sed -re 's/:0+/:/g;s/::+/::/g')
  echo "$a[$b]$c"
done

Outputs:

ipv6 Thu Nov 16 00:05:34 PST 2017 [2603:1c2:1800:a8c0::1] foo bar baz

But that is a very specific solution.

Regards,
- Robert



More information about the PLUG mailing list