Select and sort IP address keeping the whole line

9,964

Solution 1

This script copies the ip address from field 3 using awk to the start of the line with a "%" separator, then does the sort on the ip address now in the first field, then removes the added part.

awk '{print $3 " % " $0}' |
sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 |
sed 's/[^%]*% //'

If the field with the ip address is not a constant, you can auto-detect it on each line. Replace the awk above with:

awk '{ for(i=1;i<=NF;i++)
         if($i~/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)break;
       print $i " % " $0
     }' |

Solution 2

Late answer, but it might help someone. If you have a recent version of GNU sort (from GNU coreutils 7.0 or later), you can use the --version-sort (or -V) option, which will do the right thing with IPv4 addresses. Assuming input of:

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

Running this through sort -k 3 -V will yield:

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
Share:
9,964

Related videos on Youtube

Uhciar
Author by

Uhciar

Updated on September 18, 2022

Comments

  • Uhciar
    Uhciar almost 2 years

    So I need to sort the IP Addresses and then sort my line by them.

    I can sort IP addres in a file by using : sort -n -t . -k1,1 -k2,2 -k 3,3 -k4,4

    If my file looks like :

        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
    

    But in this case, field 1 will be :

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

    And field 4 will be :

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

    How and What tools should I use to sort my IP addresses and then sort my lines by them. Thanks in advance.

    EDIT : Example modfied. There is several line with the same IP address but with different text and in a random order.

    • Uhciar
      Uhciar almost 9 years
      I understand -k allow me to select start field and stop field with optionnal character. The method I know to sort the IP field will only keep the IP and not the entire line. What I want to do is select the IP field, sort them and print the line by this order.
    • ctrl-alt-delor
      ctrl-alt-delor almost 9 years
      in your example field one is add method1 a (for first record).
    • Uhciar
      Uhciar almost 9 years
      Yes I know, I edit the file forgetting this part.
  • meuh
    meuh almost 9 years
    I added a change to cope with the ip field position being variable.
  • Uhciar
    Uhciar almost 9 years
    Thanks for your help, I know -t is used to modify the filed separator, that's why when you have only IP addresses on the line, it sort them correctly. It seems a little bit complexe to remove the dot from everywhere except those which were already there for exemple if i have other numbers instead of letters on other fileds.
  • Uhciar
    Uhciar almost 9 years
    Thanks for your help.That's exactly the kind of trick I was looking for. I will learn about awk then.
  • Uhciar
    Uhciar almost 9 years
    Your answer is quite interesting for IPv4 addresses and I keep it in mind. I keep @meuh anwser since I can handle ipv6 with the same logic.
  • ChrisDR
    ChrisDR over 8 years
    This is a great trick! When did the -V option get included in GNU sort?
  • 4wk_
    4wk_ almost 6 years
    Nice one, that's perfect. @ChrisDR see unix.stackexchange.com/questions/131486/… coreutils sort v7.0 (2008-10-05)