行全体を維持しながらIPアドレスを選択してソートします。

行全体を維持しながらIPアドレスを選択してソートします。

したがって、IPアドレスを並べ替え、それに基づいて行を並べ替える必要があります。

以下を使用してファイルのIPアドレスを並べ替えることができます。sort -n -t . -k1,1 -k2,2 -k 3,3 -k4,4

私のファイルが次のような場合:

    add method1 a.b.c.d other thing
    add method2 e.f.g.h other thing2
    add method2 a.b.c.d other thing2
    add method5 j.k.l.m other thing5
    add method3 a.b.c.d other thing3
    add method1 e.f.g.h other thing2

ただし、この場合、フィールド1は次のようになります。

    add method1 a
    add method2 e
    add method2 a
    add method5 j
    add method3 a
    add method1 e

フィールド 4 は次のようになります。

    d other thing
    h other thing2
    d other thing2
    m other thing5
    d other thing3
    h other thing2

私のIPアドレスをソートしてからそれに基づいて私のラインをソートするにはどうすればいいですか?よろしくお願いします。

編集:はいが修正されました。 IPアドレスは同じですが、テキストが異なり、順序がランダムな複数行があります。

ベストアンサー1

回答が遅れましたが、誰かに役立つことがあります。 GNUソートの最新バージョンがある場合(GNUコアユーティリティ7.0以上)を使用できます。--version-sort(または-V)オプションを選択すると、IPv4アドレスに対して正しい操作が行われます。入力を仮定すると:

add method1 10.1.2.3 other thing
add method2 10.10.20.30 other thing2
add method2 10.1.2.3 other thing2
add method5 10.2.8.9 other thing5
add method3 10.1.2.3 other thing3
add method1 10.10.20.30 other thing2

これを実行すると、次のsort -k 3 -Vものが生成されます。

add method1 10.1.2.3 other thing
add method2 10.1.2.3 other thing2
add method3 10.1.2.3 other thing3
add method5 10.2.8.9 other thing5
add method1 10.10.20.30 other thing2
add method2 10.10.20.30 other thing2

おすすめ記事