3

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.

  • man sort would tell you about the -k flag – roaima Aug 28 '15 at 8:52
  • 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. – Uhciar Aug 28 '15 at 9:16
  • in your example field one is add method1 a (for first record). – ctrl-alt-delor Aug 28 '15 at 9:18
  • Yes I know, I edit the file forgetting this part. – Uhciar Aug 28 '15 at 9:19
4

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
     }' |
  • I added a change to cope with the ip field position being variable. – meuh Aug 28 '15 at 10:04
  • Thanks for your help.That's exactly the kind of trick I was looking for. I will learn about awk then. – Uhciar Aug 28 '15 at 10:11
6

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
  • 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. – Uhciar Aug 29 '15 at 12:30
  • This is a great trick! When did the -V option get included in GNU sort? – ChrisDR Dec 21 '15 at 7:13
  • Nice one, that's perfect. @ChrisDR see unix.stackexchange.com/questions/131486/… coreutils sort v7.0 (2008-10-05) – 4wk_ Aug 1 '18 at 8:48
0

This is expected, make sure you understand the -t option (man sort: field separator). The command you wanted to use deals with plain ip addresses only.

A quick and dirty solution might be to convert the spaces into dots . the file first and then sort (you may want to undo the conversion later, excluding the IP addresses)

sed -i.bak 's/ /./g' data.log
sort -n -t . -k2,2 -k3,3 -k4,4 -k5,5 data.log

Note that I shifted the indices. Give it a try.

  • 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 Aug 28 '15 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.