Hello my friends, I need to count how many words are into a log file, I'm using:
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?
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..
#!/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}
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.