The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to make a line BLINKING in output and also how to increase font size in output mail2sant Shell Programming and Scripting 3 04-14-2008 04:30 AM
SU issues yoi2hot4ya Shell Programming and Scripting 1 10-09-2007 08:11 AM
Tar issues! HELP! Slaughter UNIX for Dummies Questions & Answers 3 04-19-2006 09:22 AM
AIX 5.3 Issues miket AIX 1 04-26-2005 02:12 PM
dns issues rickyt00 UNIX for Advanced & Expert Users 1 03-22-2005 07:18 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-15-2008
Registered User
 

Join Date: May 2008
Posts: 53
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Awk output issues.

I have the follwing code:

Code:
awk '{print $1}' HITS                                   #Searches HITS file column one. Column one is filenames

awk '{print $2}' HITS  | sort -n | wc -l            #Searches HITS file and sorts numerically and outputs line count. column 2 is IP addresses

awk '{print $2}' HITS | uniq | wc -l                # Searches HITS file for unique entries and outputs line count. column 2 is IP addresses
i know my code is not right and my results are not listed how i want it. they are just displayed one after the other on seperate lines.

like this:
Code:
hits/adverts.hits:248.204.125.183
hits/mags.hits:87.114.172.31
hits/adverts.hits:34.220.19.30
hits/food.hits:185.227.145.86
hits/food.hits:213.225.8.140
hits/mags.hits:83.222.98.178
hits/food.hits:118.195.119.35
10345
245
What I want them to all be on the same line in a table to look like this:

Code:
FILENAME                    HITS                       UNIQUE HITS
food.hits                   2034                            245
mags.hits                   2000                            435
adverts.hits                1456                            344

#the hits column needs to also be in a descending order as shown above

Last edited by amatuer_lee_3; 05-16-2008 at 01:04 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-15-2008
Registered User
 

Join Date: Jul 2007
Posts: 72
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
may be you should provide input data which gives clarity on your requirement.

-ilan
Reply With Quote
  #3 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 53
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
There wont be any input data i just want the function to display the results.
Reply With Quote
  #4 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 233
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Did you know it's spelt 'amateur'?

Some comments regarding your existing code:

Code:
awk '{print $1}' HITS

# you need to specify the column separator because awk uses 
# white space (spaces/tabs) by default , e.g.

awk -F: '{print $1}' HITS
Code:
awk '{print $2}' HITS  | sort -n | wc -l

# no need for awk and sort, as it doesn't change the number of lines of data, just:

wc -l < HITS
Code:
 awk '{print $2}' HITS | uniq | wc -l

# if the data is unsorted uniq does not identify matching lines, better to use:

awk -F: '{print $2}' HITS | sort -u | wc -l
Personally I would use one awk script to generate all of the results, something like:

Code:
sort -t : -k 1,1 -k 2,2 HITS | awk -F: '
        # assign values to variables for readability, count a hit
        { file=$1; ip=$2; hits[file]++ }
        # initialise prevfile when reading the first line
        NR==1 { prevfile=file }
        # if it is a new file, reset the previous IP
        file != prevfile { previp="" }
        # if the ip is different to the previous IP, count a unique hit
        ip != previp { uniquehits[file]++ }
        # save previous ip and file name for future reference
        { previp=ip; prevfile=file }
        # output the results
        END { for (file in hits) { print file,hits[file],uniquehits[file] } }
'
This won't output in exactly the format you wanted, you can use printf() for that, but I'll leave that part as an exercise for you!
Reply With Quote
  #5 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 53
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thanks very much. Yeah i do know its spelt wrong. I noticed after i clicked confirm on my username. just cant be bothered to change it.

can i refer you to my recent post:

using uniq and awk??

This gives a better explanation as to what problems i have. This is kind of irrelevant now because i misinterpreted my requirements. But this still does help me. Thanks again.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 04:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101