Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Dear All, Need a help with one problem i m facing. I have a file which has a series of IP addresses in 1st column and no.of hits by that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    6

    Need help with sorting !!


    Dear All,
    Need a help with one problem i m facing.
    I have a file which has a series of IP addresses in 1st column and no.of hits by that IP in the 2nd column. as below

    97.208.3.25 357
    97.233.255.196 791
    97.233.255.198 703
    97.233.255.202 694
    97.233.255.205 8
    97.233.255.210 406
    97.233.255.211 845
    97.233.255.212 668
    97.233.255.215 384
    97.233.255.217 419
    97.233.255.223 775
    97.233.255.226 344
    97.233.255.232 660
    97.233.255.242 50
    97.233.255.250 483
    97.233.255.251 824
    97.208.3.25 73
    97.233.255.196 323
    97.233.255.198 276
    97.233.255.202 275
    97.233.255.205 431
    97.233.255.211 387
    97.233.255.212 351
    97.233.255.215 510
    97.233.255.217 147

    I want to script it such that i get the max hit value of unique IP addresses.

    for eg .IP 97.233.255.217 has got two values 419 & 147. in this i should get the IP and value 419 only.Similarly for IP 97.233.255.215 i shud get the value 510 and the IP only.
    Hope my question is clear. Also i need it in bash.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,227
    Code:
    for a in `awk '{ print $1 }' <PATH_TO_LIST_FILE> | sort | uniq`; do grep  $a <PATH_TO_LIST_FILE> | sort -rn -k2 | head -n 1 ; done
    Getīs the job done, but this is terrible ineffective code.
    It can surely be done with awk alone, maybe someone more experienced with awk can give some advise
    Last edited by Irithori; 09-27-2010 at 12:15 PM.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    6
    Cool script dude...its working....
    Thanks a lot !!

  4. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,227
    yw.
    It should maybe be rewritten using either only perl or only awk.
    You must always face the curtain with a bow.

  5. #5
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    awk 'END {
      for (IP in ip) 
        print IP, ip[IP]
      }
    $2 > ip[$1] {
      ip[$1] = $2
      }' infile

  6. #6
    Just Joined!
    Join Date
    Sep 2010
    Posts
    6
    @Irithori
    yup i know....but i m weak in perl scripting so dint attempt it ....
    also ur script worked but it goes in a loop ...so i took that output in another file and then used uniq on it....

  7. #7
    Just Joined!
    Join Date
    Sep 2010
    Posts
    6
    @radoulov

    thanks for this script....it works efficiently...
    needed one more addition....
    what if i have a 3rd and 4th column as hour and min and i want the hour and minute output also when the max count had occurred.
    for eg
    97.233.255.217 419 12 52
    97.233.255.217 198 12 30

    in this it should diplay the 3rd and 4th column also.

  8. #8
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    awk 'END {
      for (IP in ip) 
        print IP, ip[IP]
      }
    $2 > ip[$1] {
      ip[$1] = $2 FS $3 FS $4
      }' infile

  9. #9
    Just Joined!
    Join Date
    Sep 2010
    Posts
    6
    @radoulov
    Hi it is giving a wrong output when in include $3 & $4 also.
    the first code worked properly.

    I have uploaded the raw data(sample.zip) for your reference if needed.
    Attached Files Attached Files

  10. #10
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,227
    ..or you can try and modify it yourself for your needs.
    You must always face the curtain with a bow.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •