how to sort, and count unique data all at once? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

how to sort, and count unique data all at once?

Shell Programming and Scripting


Tags
linux, solaris, ubuntu

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-15-2008
amatuer_lee_3 amatuer_lee_3 is offline
Registered User
 
Join Date: May 2008
Last Activity: 17 May 2008, 10:34 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
how to sort, and count unique data all at once?

Quote:
echo "Number of unique I.P.'s: "

uniq $2 HITS | wc -l
I have this code.

In the file called HITS.hits my results are displayed liked this:

filename.hits: 35.114.42.113 Mon Nov 21 08:26:41 GMT 2005

What i want my code to do is to search all of the unique IP addresses.
so count all the adresses that appear.
but just count repeat IPs that are the same as 1.

so if my file had:

filename.hits: 11.111.11.111
filename.hits: 33.333.33.333
filename.hits: 22.222.22.222
filename.hits: 44.33.222.111
filename.hits: 11.111.11.111

the answer would be 4.
Sponsored Links
    #2  
Old 05-15-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 30 October 2014, 12:48 PM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
One possibility:


Code:
awk '{a[$2]}END{for(i in a){n++};print n}' file

Regards
Sponsored Links
    #3  
Old 05-15-2008
amatuer_lee_3 amatuer_lee_3 is offline
Registered User
 
Join Date: May 2008
Last Activity: 17 May 2008, 10:34 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
menu script

ok i have tried this and it didnt work :s

maybe i just entered it wrong.

i entered:

Quote:
awk '{a[$2]}END{for(i in a){n++};print n}' HITS
my filename is HITS that i want to search. as you quoted in your code solution. now it wont even run. i click to run in terminal and it just closes straight away
    #4  
Old 05-15-2008
alamitab alamitab is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 11 February 2010, 10:59 AM EST
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
number of unic lines:
cat file| awk ' {print $2}' | sort | uniq | wc -l
or show the lines:
cat file| awk ' {print $2}' | sort | uniq
Sponsored Links
    #5  
Old 05-15-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 30 October 2014, 12:48 PM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
Which OS?
Use nawk or /usr/xpg4/bin/awk on Solaris.

REgards
Sponsored Links
    #6  
Old 05-15-2008
amatuer_lee_3 amatuer_lee_3 is offline
Registered User
 
Join Date: May 2008
Last Activity: 17 May 2008, 10:34 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
using ubuntu, and suse10.3
Sponsored Links
    #7  
Old 05-15-2008
amatuer_lee_3 amatuer_lee_3 is offline
Registered User
 
Join Date: May 2008
Last Activity: 17 May 2008, 10:34 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
ok basically i am running the script so i dont think i need to cat the file as when i get to the point where i want this function the HITS file is already created so i just need to search the file to get the results.

either way i tried
Quote:
cat HITS| awk ' {print $2}' | sort | uniq | wc -l
and also

Quote:
awk '{print$2}' HITS | sort | uniq | wc -l
and even

Quote:
awk '{print$2}' | sort | uniq | wc -l HITS
none of which work.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unique sort with two fields dealerso Shell Programming and Scripting 3 04-26-2011 07:27 AM
Awk sort and unique greycells Shell Programming and Scripting 3 02-15-2011 02:33 PM
What some ideas with sort and get unique data asirohi Shell Programming and Scripting 1 08-19-2009 10:44 AM
How to count unique strings my_Perl Shell Programming and Scripting 6 07-31-2009 04:28 AM
Sort and Unique in Perl deepakwins Shell Programming and Scripting 20 02-11-2008 08:25 PM



All times are GMT -4. The time now is 07:49 AM.