Word Count for a range of text


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Word Count for a range of text
# 1  
Old 10-31-2008
Word Count for a range of text

Help Please!

I have alot of text that I would like to count for a range of text.

logevents.1:<190> Oct 30 02:59:42 N/A [23]: 6|4145|RC|CAC: Terminated
logevents.1:<190> Oct 30 02:59:42 N/A [23]: 6|4097|RC|CAC: Deleted
logevents.1:<190> Oct 30 02:59:44 N/A [36]: 6|4145|RC|CAC: Terminated
logevents.1:<190> Oct 30 02:59:44 N/A [36]: 6|4097|RC|CAC: Deleted
logevents.1:<190> Oct 30 02:59:46 N/A [23]: 6|4096|RC|CAC: Created
logevents.1:<190> Oct 30 03:00:01 N/A [36]: 6|4096|RC|CAC: Created
logevents.1:<190> Oct 30 03:00:05 N/A [23]: 6|4145|RC|CAC: Terminated
logevents.1:<190> Oct 30 03:00:05 N/A [23]: 6|4097|RC|CAC: Deleted
logevents.1:<190> Oct 30 03:00:06 N/A [36]: 6|4145|RC|CAC: Terminated
logevents.1:<190> Oct 30 03:00:06 N/A [36]: 6|4097|RC|CAC: Deleted
logevents.1:<190> Oct 30 03:00:06 N/A [23]: 6|4145|RC|CAC: Terminated

I would like to count how many times per hour the word Created showed up.
So I guess I am looking for something that will search on Oct 30 00 through Oct 30 23 that also had the word Created on that line.

any help would be great, thanks
Brian
# 2  
Old 10-31-2008
Assuming no trailing spaces in the above file:
Code:
grep 'Created$' filename | grep 'Oct 30' | wc -l | read cnt
echo " $cnt /24 " | bc -l

This gives an average per hour - is that what you want?
# 3  
Old 10-31-2008
Hammer & Screwdriver

I added a couple more "Created" into my file, so I could see more/better results.

Code:
> awk '$8=="Created" {print $2,$3,substr($4,1,2),$8}' file27
Oct 30 02 Created
Oct 30 03 Created
Oct 30 03 Created
Oct 30 04 Created
Oct 30 04 Created
Oct 30 04 Created
Oct 30 04 Created

Above shows the "Created" in a few more lines

Code:
> awk '$8=="Created" {print $2,$3,substr($4,1,2),$8}' file27 | sort | uniq -c
      1 Oct 30 02 Created
      2 Oct 30 03 Created
      4 Oct 30 04 Created

Above give a count, then the month day hour
# 4  
Old 10-31-2008
something like:

Code:
 awk '/Oct 30.*Created/{c++}END{print c}' file1

or more simply

Code:
grep -c "Oct 30.*Created" file1

should suffice
# 5  
Old 10-31-2008
joeyg
That worked perfectly
Thanks a bunch
Brian
# 6  
Old 10-31-2008
Use nawk or /usr/xpg4/bin/awk on Solaris:


Code:
awk 'END { 
  print h, c 
  }
!_[$2 FS $3 FS substr($4,1,2)]++ && NR > 1 { 
  print h, c 
  c = 0 
  }
{ 
  h = $2 FS $3 FS substr($4,1,2)
    if ($NF ~ /Created/) 
      c++ 
  }' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Get user id count for a range

I have a file which has users having different values against them in second column. I want to set a range and get the count of users in that range. Suppose :- a 4 b 19 c 27 d 87 e 123 And I define the range like 1-20 , 20 -50 and so on..... Please assume the... (1 Reply)
Discussion started by: Sanjeev Yadav
1 Replies

3. Shell Programming and Scripting

Count occurences of a numeric string falling in a range

Dear all, I have numerous dat files (1.dat, 2.dat...) containing 500 numeric values each. I would like to count them, based on their range and obtain a histogram or a counter. INPUT: 1.dat 1.3 2.16 0.34 ...... 2.dat 1.54 0.94 3.13 ..... ... (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

4. UNIX for Dummies Questions & Answers

How to count how many numbers in a certain range?

Hi I have a data file with two columns which looks like: 1 42 2 40 3 55 4 50 5 38 6 49 7 33 8 46 9 39 10 33 11 33 12 26 13 46 14 44 15 55 16 54 17 30 18 32 (7 Replies)
Discussion started by: marhuu
7 Replies

5. UNIX for Dummies Questions & Answers

Separate text files in a folder by word count

Hi, been searching Google for like an hour and I doubt I got the right keywords so might as well ask here. What I need: Before: Article1.txt 564 Article2.txt 799 Article3.txt 349 Article4.txt 452 * Separate files over 400 wordcount * After: Article1.txt 564... (3 Replies)
Discussion started by: Richard2000
3 Replies

6. UNIX for Advanced & Expert Users

Word count

Script that lists all words used in one or more files and displays their count (pattern /\W+/ to split the lines of the input file into words can b used).. It should display list in format word:count...gets Filename as an cmd line argument! eg: $perl test doc (where doc is d file we are going to... (4 Replies)
Discussion started by: aadi_uni
4 Replies

7. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

8. UNIX for Dummies Questions & Answers

Word Count

Hi All, Please let me know how to get the count of a particular word in a file. Example. I am looking for count of word 'result' in a file abc.xml. Thanks, Shankar (10 Replies)
Discussion started by: s_chowhan
10 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

10. UNIX for Dummies Questions & Answers

count word

hi, given a file i need to get the first line and secodn line and count each of the line whether the length of first line and second line is the same i don;t know how to get the length of the line...seems like use 'wc' cannot do it... please advice (1 Reply)
Discussion started by: ariuscy
1 Replies
Login or Register to Ask a Question