Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 05-15-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 May 2015, 9:42 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 129
Thanked 544 Times in 512 Posts
One possibility:


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

Regards
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 05-15-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 May 2015, 9:42 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 129
Thanked 544 Times in 512 Posts
Which OS?
Use nawk or /usr/xpg4/bin/awk on Solaris.

REgards
Sponsored Links
    #6  
Old Unix and Linux 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 05:13 AM.