3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

省略表記のIPv6をフル表記に変換するシェル

Last updated at Posted at 2016-08-16

概要

ファイルの特定の列に含まれているIPv6をフル表記にしたい。

実行結果

(時間、ipv6、メッセージ)のセットのデータを、(時間時間、ipv6、ipv6フル表記、メッセージ)のセットへ変換する。

・入力ファイル

sample.txt
2016/08/08 22:00:00  2001:0db8:bd05:01d2:288a:1fc0:0001:10ee  aaa
2016/08/08 22:01:00  2001:0db8:0020:0003:1000:0100:0020:0003  bbb
2016/08/08 22:02:00  2001:db8:20:3:1000:100:20:3              ccc
2016/08/08 22:03:00  2001:0db8:0000:0000:1234:0000:0000:9abc  ddd
2016/08/08 22:04:00  2001:db8::1234:0:0:9abc                  eee
2016/08/08 22:05:00  2001:db8::9abc                           fff
2016/08/08 22:06:00  127.0.0.1                                ggg

・変換用シェル

GetIPv6FullAddress.sh
#!/bin/sh

inf=$1

cat ${inf}                                                  |
awk '                                                       \
    # v6フル表記変換                                          \
    function v6Full(v6ip, v6full, cnt, array, i ) {         \
        cnt = split(v6ip, array, ":" ) ;                    \
        if(cnt == 1) { return v6ip ; }                      \
        for(i=0; i<=8-cnt; i++) {                           \
            sub("::", ":0000::", v6ip) ;                    \
        }                                                   \
        sub("::", ":", v6ip) ;                              \
        cnt = split(v6ip, array, ":") ;                     \
        for(i=1; i<=8; i++) {                               \
            v6full = sprintf("%s%04s:", v6full, array[i]) ; \
        }                                                   \
        sub(":$", "", v6full) ;                             \
        return v6full ;                                     \
    }                                                       \
    # メイン処理                                               \
    {                                                       \
        fullip = v6Full($3, a, cnt, array, i) ;             \
        printf("%s %s %-39s %-39s %s\n", $1, $2, $3, fullip, $4) ; \
    }                                                       \
'

・実行結果

$ sh v6FullAddress.sh sample.txt 
2016/08/08 22:00:00 2001:0db8:bd05:01d2:288a:1fc0:0001:10ee 2001:0db8:bd05:01d2:288a:1fc0:0001:10ee aaa
2016/08/08 22:01:00 2001:0db8:0020:0003:1000:0100:0020:0003 2001:0db8:0020:0003:1000:0100:0020:0003 bbb
2016/08/08 22:02:00 2001:db8:20:3:1000:100:20:3             2001:0db8:0020:0003:1000:0100:0020:0003 ccc
2016/08/08 22:03:00 2001:0db8:0000:0000:1234:0000:0000:9abc 2001:0db8:0000:0000:1234:0000:0000:9abc ddd
2016/08/08 22:04:00 2001:db8::1234:0:0:9abc                 2001:0db8:0000:0000:1234:0000:0000:9abc eee
2016/08/08 22:05:00 2001:db8::9abc                          2001:0db8:0000:0000:0000:0000:0000:9abc fff
2016/08/08 22:06:00 127.0.0.1                               127.0.0.1                               ggg

参考文献

下記参考にさせていただきました。
http://blog.n-z.jp/blog/2014-06-14-jus-usp.html

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?