Help with scritp to count an specific word into a log | 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.

Help with scritp to count an specific word into a log

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-22-2006
lestat_ecuador lestat_ecuador is offline
Registered User
 
Join Date: Oct 2006
Last Activity: 26 August 2009, 6:43 PM EDT
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Help with scritp to count an specific word into a log

Hello my friends, I need to count how many words are into a log file, I'm using:

Code:
cat logfile | grep 'word' | wc -l

Cuz the 'word' appears once per line.

But my logfile grow faster and at the end ofthe day is really big, so how i can count the 'word' only from (by example) line 4000 of the logfile til the end, then get the last number of line (x) and in the next time count from 'x' line til the new end (x'), that has sence?

Thanx for your help.

Lestat
Sponsored Links
    #2  
Old 10-22-2006
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
what you can do is capture the line count to a file.
then whenever you run the script, get the value into a shell variable. with this variable, you can do your processing..
Sponsored Links
    #3  
Old 10-23-2006
vish_indian vish_indian is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 9 July 2012, 5:00 AM EDT
Location: Delhi, India
Posts: 95
Thanks: 0
Thanked 1 Time in 1 Post
Firstly, you don't need to use cat and wc -l while searching. You can simply use

Code:
 grep -c 'word'  logfile


For searching from a particular line number onwards, you can save the line number in a variable and pass it to gawk or nawk:

Code:
 awk -v num=${saved_line_no} -v word=${word} 'NR>num && $0~word{ count++} END{ print count}' logfile

    #4  
Old 10-23-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 27 October 2014, 1:32 PM EDT
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
You can do something like this :

Code:
#!/usr/bin/ksh

#
# Define logile and searched word
#

log_file=logfile
searched_word=word
prv_line_count_file=${log_file}.prv_count

#
# Get previous line count
#

if [ -f ${prv_line_count_file} ]
then
   prv_line_count=$(<${prv_line_count_file})
else
   prv_line_count=0
fi

#
# Get actual line count and memorize it
#

line_count=$(awk 'END {print NR}' ${log_file})
echo ${line_count} > ${prv_line_count_file}

#
# If logfile is smaller, start searching at the first line
#

if [ ${prv_line_count} -gt ${line_count} ]
then
   prv_line_count=0
fi

#
# Search for word
#

awk -v num=${prv_line_count} \
    -v word="${searched_word}" \
    'NR>num && $0~word { count +=1 } END { print count+0 }' \
    ${log_file}

jean-Pierre.
Sponsored Links
    #5  
Old 09-07-2007
DaddyShortLegs DaddyShortLegs is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 7 September 2007, 7:13 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
You could do something much simpler:

tail -1000 filename | grep 'word' | wc -l

This will count the the number of lines that word appears in the last 1000 lines of the file (or less if there are not 1000 lines in the file).

So, you could keep a track of the line that you last searched up to, and get the number of lines (wc -l filename) and stick the difference into the -1000 bit.
Sponsored Links
    #6  
Old 09-07-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
you have revived an old thread. pls check before posting.
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
How to print line starts with specific word and contains specific word using sed? tmalik79 UNIX for Dummies Questions & Answers 11 10-11-2011 06:17 AM
Count specific word or character per line janzper UNIX for Advanced & Expert Users 1 05-27-2011 03:42 AM
Need script to count specific word and iterate over number of files. elamurugu Shell Programming and Scripting 2 11-27-2010 02:54 AM
Word count of lines ending with certain word warlock129 Shell Programming and Scripting 8 08-30-2009 04:37 AM
How to count the occurences of a specific word in a file in bash shell mskart UNIX for Dummies Questions & Answers 2 10-30-2008 01:42 PM



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