[PLUG] Fun sed challenge!

Russell Senior russell at personaltelco.net
Thu Nov 16 14:41:26 UTC 2017


On Thu, Nov 16, 2017 at 6:05 AM, Robert Citek <robert.citek at gmail.com> wrote:
> 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.?

Embedded platform doesn't necessarily have any of those available,
and/or i don't want to take the CPU and memory hit of loading it all
even if it is available.

>
> 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'

That's nice and compact!  ;-)

>
> 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

I only object that you didn't string it all along one line! ;-)

>
> 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
> _______________________________________________
> PLUG mailing list
> PLUG at pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug



More information about the PLUG mailing list