The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find total size for some files? helen008 UNIX for Dummies Questions & Answers 5 06-09-2008 12:01 PM
sort files by date, delete oldest, if total size bigger than scarfake Shell Programming and Scripting 2 05-21-2008 08:02 AM
total occ. gander_ss Shell Programming and Scripting 6 05-09-2008 03:54 AM
total number of files which have "aaa" in files whose names are File*_bbb* sudheshnaiyer UNIX for Dummies Questions & Answers 1 08-16-2007 02:34 PM
Running Total lyoncc Shell Programming and Scripting 9 08-07-2007 11:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-07-2007
MrAd MrAd is offline
Registered User
  
 

Join Date: May 2007
Posts: 6
grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to :

adventure.hits:6 adverts.hits:16 blog.hits:19 books.hits:8 cds.hits:7 contact.hits:2 crime.hits:11 dvds.hits:8 fantasy.hits:9 fiction.hits:12 food.hits:10 gadgets.hits:14 games.hits:21 gardening.hits:6 health.hits:13 hotlist.hits:20 index.hits:83 jobs.hits:4 mags.hits:14 music.hits:9 news.hits:5 nonfiction.hits:16 novels.hits:9 people.hits:10 recent.hits:17 sales.hits:4 sf.hits:16 software.hits:18 sport.hits:13 test.hits:6

is there a simple way to get grep to skip all the above information and just give the final total of occurences across all of the files?

I know it's probably staring me in the face but I just cant get my head around a simple and effective way to do it.

Regards.
  #2 (permalink)  
Old 05-07-2007
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
I would use awks to maintain the totals, and print out the results in the END paragraph - need more information on the file format, and what you are grepping for to duplicate in awk.
  #3 (permalink)  
Old 05-07-2007
MrAd MrAd is offline
Registered User
  
 

Join Date: May 2007
Posts: 6
basically I have narrowed the file down into a list of IP address. then I search through each of the hit files using awk and grep

a .hit file contains an entry per line:

192.168.2.1 Tue nov 22 20:30:45 GMT 2005

The code I have so far is:

IPList is a list of IP addresses

cat IPList | while read LINE
do
TOTALHITS=`grep -c "$LINE" *.hits`
echo $TOTALHITS $LINE >> totalIPHits
done

but the output of the grep command is as shown in the original post. how would I go about awking such a statement to keep total?
  #4 (permalink)  
Old 05-07-2007
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
something like this maybe

awk '{ Array[$1]++ }
END{ for (I in Array)
{ print I, Array[I]}
}' *.hit

This will scan multiple files that would be shown with an ls *.hit listing, then, using the first field as its key, maintain a total of the hits in the Array.

then, at the End, for each value in the array, print the Array Key followed by the count.
  #5 (permalink)  
Old 05-07-2007
MrAd MrAd is offline
Registered User
  
 

Join Date: May 2007
Posts: 6
ah I'll give that a try, thanks
  #6 (permalink)  
Old 05-08-2007
kapilraj kapilraj is offline
Registered User
  
 

Join Date: Dec 2006
Location: Maryland
Posts: 162
or simply ,

cat file1 file2 file3 file4 ............... | grep -c <ip_address>
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0