Determining Word Frequency of Specific Terms


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determining Word Frequency of Specific Terms
# 1  
Old 03-05-2009
Determining Word Frequency of Specific Terms

Hello,
I require a perl script that will read a .txt file that contains words like

224.199.207.IN-ADDR.ARPA. IN NS NS1.internet.com.
4.200.162.207.in-addr.arpa. IN PTR beeriftw.internet.com.
arroyoeinternet.com. IN A 200.199.227.49

I want to focus on words:
IN NS
IN PTR
IN A
IN CNAME

I like to get a output that looks like:

Total number of NS records =
Total number of PTR records=
Total number A records=
Total number of CNAME=

Thanks in advance
# 2  
Old 03-05-2009
Code:
#!/usr/bin/perl -n
# cnt.pl	
	my $ns = 0;
	my $ptr = 0;
	my $a = 0;
	my $cname = 0;
	while(<>)
	{
	  if (/IN NS/   ) {$ns++;   }
               if (/IN PTR/  ) {$ptr++;  }
               if (/IN A/    ) {$a++;    }
               if (/IN CNAME/) {$cname++;}
             }

     print "NS    records =", $ns   , "\n";
     print "PTR   records =", $ptr  , "\n";
     print "A     records =", $a    , "\n";
     print "CNAME records =", $cname, "\n";

usage: cnt.pl < logfile

FWIW this is really not a perl type thing - awk is probably better IMO.
# 3  
Old 03-05-2009
Or:

Code:
perl -ane'
  $_{$F[2]}++;
  print map "Total number of $_ records:\t$_{$_}\n", 
    keys %_ if eof
  ' infile

With AWK:

(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'END {
  for (k in _) 
    printf "Total number of %s:\t%d\n", k, _[k]
	}
{ _[$3]++ }' infile


Last edited by radoulov; 03-05-2009 at 04:12 PM..
# 4  
Old 03-05-2009
I have many zone files or dns zones that contain various record types.

is it to much to ask to add some finesse to my request.

Example: I could have

db.208.199.11.0

That would contain the below information

Code:
224.199.207.IN-ADDR.ARPA. IN NS AIM1.internet.com.
4.200.162.207.in-addr.arpa. IN PTR beeriftw.internet.com.
arroyoeinternet.com. IN A 200.199.227.49

Then another file
db.explorer.com would contain

Code:
224.162.207.IN-ADDR.ARPA.       IN NS   pwedns1.internet.com.
224.162.207.IN-ADDR.ARPA.       IN NS   pmedns1.internet.com.
224.162.207.IN-ADDR.ARPA.       IN NS   phedns1.internet.com.
224.162.207.IN-ADDR.ARPA.       IN NS   auth100.ns.aut.net.

So what I am requesting is to create input file that has these names in it that would use your script to count against.

So the output may look like for each word in my input file

Code:
db.208.199.11.0:
Total number of A records = 684
Total number of PTR records = 306
Total number of CNAME records = 58
Total number of NS records = 1352

db.explorer.com;
Total number of A records = 6
Total number of PTR records = 30
Total number of CNAME records = 88
Total number of NS records = 55

So rather then having it look for each txt file like my original thought, is have the script reference a master input file.

Thanks in advance !

Last edited by radoulov; 03-06-2009 at 08:22 AM.. Reason: added code tags
# 5  
Old 03-06-2009
No need to create a master input file, AWK (or Perl, whichever you prefer) could process multiple input files. So assuming all your files reside in the same directory and all filenames begin with the string db:

Code:
awk 'END {
  print f ":"
    for (Z in z)
      printf "Total number of %s records = %d\n", \
      Z, z[Z]
    print RS
    }
FNR == 1 {
  if (f) {
    print f ":"
    for (Z in z)
      printf "Total number of %s records = %d\n", \
      Z, z[Z]
    print RS
    }
    f = FILENAME
  }    
{ z[$3]++ }' db*

# 6  
Old 03-06-2009
Hi Thanks for your reply, I ran your code, the out put looks like:

db.255.0.0.0:
Total number of SOA records = 17
Total number of records = 187
Total number of Serial records = 17
Total number of NS records = 17
Total number of Retry records = 17
Total number of OF records = 17
Total number of PTR records = 166
Total number of ; records = 17
Total number of Refresh records = 17
Total number of from: records = 17
Total number of FILE records = 68
Total number of Expire records = 17

Its spitting out alot of stuff, not sure what some mean like :
Total number of ; records = 17
Total number of Expire records = 17

Where is it getting that from? and can we tweak it?
# 7  
Old 03-06-2009
Yes,
it seems that not all records have the same format. Could you post a bigger sample of your data that includes records containing the offending patterns (Serial, Retry, Expire etc.)?

Perhaps something like this will be sufficient:

Code:
awk 'END {
  print f ":"
    for (Z in z)
      printf "Total number of %s records = %d\n", \
      Z, z[Z]
    print RS
    }
FNR == 1 {
  if (f) {
    print f ":"
    for (Z in z)
      printf "Total number of %s records = %d\n", \
      Z, z[Z]
    print RS
    }
    f = FILENAME
  }    
$2 == "IN" { z[$3]++ }' db*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

Count frequency of unique values in specific column

Hi, I have tab-deliminated data similar to the following: dot is-big 2 dot is-round 3 dot is-gray 4 cat is-big 3 hot in-summer 5 I want to count the frequency of each individual "unique" value in the 1st column. Thus, the desired output would be as follows: dot 3 cat 1 hot 1 is... (5 Replies)
Discussion started by: owwow14
5 Replies

3. Shell Programming and Scripting

Shell scripting: frequency of specific word in a string and statistics

Hello friends, I need a BIG help from UNIX collective intelligence: I have a CSV file like this: VALUE,TIMESTAMP,TEXT 1,Sun May 05 16:13:05 +0000 2013,"RT @gracecheree: Praying God sends me a really great man one day. Gotta trust in his timing. 0,Sun May 05 16:13:05 +0000 2013,@sendi__... (19 Replies)
Discussion started by: kraterions
19 Replies

4. Shell Programming and Scripting

Convert a list of word/terms into their Regexp representation

Ok this might sound pretty weird but here is the request. Running on a linux system in bash or Perl (i really don't know perl but the end user has a few pearl script already) Start File looks something like this (4000 entries) TEST PLAN T//TF T-TF TEST (T) Hacker ... I am thinking about... (3 Replies)
Discussion started by: oly_r
3 Replies

5. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: &quot;Liberation Sans&quot;; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

6. Shell Programming and Scripting

Help with calculating frequency of specific word in a string

Input file: #read_1 AWEAWQQRZZZQWQQWZ #read_2 ZZAQWRQTWQQQWADSADZZZ #read_3 POGZZZZZZADWRR . . Desired output file: #read_1 3 #read_1 1 #read_2 2 #read_2 3 #read_3 6 . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

7. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

8. Shell Programming and Scripting

Word Frequency Sort

hello, Here is a program for creating a word-frequency # wf.gk --- program to generate word frequencies from a file { # remove punctuation: This will remove all punctuations from the file gsub(/_]/, "", $0) #Start frequency analysis for (i = 1; i <= NF; i++) freq++ } END #Print output... (11 Replies)
Discussion started by: gimley
11 Replies

9. Shell Programming and Scripting

word frequency counter - awk solution?

Dear all, i need your help on this. There is a text file, i need to count word frequency for each word with frequency >40 in each line of file and output it into another file with columns like this: word1,word2,word3, ...wordn 0,0,1 1,2,0 3,2,0 etc -- each raw represents... (13 Replies)
Discussion started by: irrevocabile
13 Replies

10. Shell Programming and Scripting

Word frequency with additional information

Hello everyone, I am using a chunk of code to display the frequency of a file name in a list of directories. The code looks like this: find . -name "*.log" | cut -d/ -f4 | cut -d. -f1 | awk '{print $1}' | sort | uniq -c | sort -nr The file paths would look something like this:... (1 Reply)
Discussion started by: ToeLint
1 Replies
Login or Register to Ask a Question