how to count a word in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to count a word in a file
# 1  
Old 07-09-2009
how to count a word in a file

dear all,

i have a requirement to count the errors and display from a file.
eg. file1.txt
Code:
sjdgfjdgfgd ora-0001 sdjgfydh sdukgh7 23
sjdgfjdgfgd ora-0002 sdjgfydhsf34 ew 34v
sjdgfjdgfgd ora-0008 sdjgfydh asdf asdfas 
sjdgfjdgfgd ora-0001 sdjgfydhjkbs ui873
sjdgfjdgfgd ora-0004 sdjgfydh 2876gfen 
sjdgfjdgfgd ora-0002 sdjgfydhj uewiuriue 324987

the output would be :
Code:
Error Code : ORA-0001  Count : 2
Error Code : ORA-0002  Count : 2
Error Code : ORA-0004  Count : 1
Error Code : ORA-0008  Count : 1

I wrote a prog. like below and is working fine. would like to know is there are any simple way to write the prog. New to unix so not sure of other ways.
Thanks in advance.

Code:
#!/bin/sh
echo "Enter filename..."
read name
cd /test/unix
cat $name | while read line
do
echo "$line" > tmpj
cat "tmpj" | egrep -c ora- > tmpk
if [ `cat tmpk` -gt 0 ]
then 
cat tmpj | sed 's/.*\(ora-.....\).*/\1/' >> tmpl
fi
done
rm tmpj
rm tmpk
for var1 in `cat tmpl`
do
echo "$var1" > tmpj
cat tmpl | egrep -c `cat tmpj` > tmpk
if [ `cat tmpk` -gt 0 ]
then
echo "Error Code : "$var1"  Count : `cat tmpk`"
sed "/$var1/d" tmpl > tmpm
mv tmpm tmpl
fi
done
rm tmpj
rm tmpk
rm tmpl


Last edited by vgersh99; 07-09-2009 at 12:11 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-09-2009
Code:
nawk '
{
   a[$2]++
}
END {
   for (i in a)
     print "Error Code : " i " Count : " a[i]
}
' file1.txt



---------- Post updated at 11:12 AM ---------- Previous update was at 11:10 AM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 07-09-2009
Hi gerh99,

Thanks, excellent code.
The file which I had is an error message file and the ora errors are not always the 2nd col./occurence. ora error may appear anywhere in the line or wont appear also.
Apologies for the inconvenience.
eg.
Code:
sjdgfjdgfgd sdjgfydh sdukgh7 23 ora-0001 
sjdgfj dgf g  d ora-0002 sdjgfydhsf34 ew 34v
sjdg fjdgf gd ora-0008 sdjgfydh asdf asdfas 
sjdgfj dgf gd ora-0001 sdjgfydhjkbs ui873
sjdgfjdg fgd sdjgfydh 2876gfen 
sj dgfjd gfgd ora-0002 sdjgfydhj uewiuriue 324987


Last edited by vgersh99; 07-09-2009 at 12:51 PM.. Reason: code tags, PLEASE!
# 4  
Old 07-09-2009
Code:
nawk '
/ora-[0-9]/ {
    for(i=1;i<=NF;i++)
       if ( $i ~ /ora-[0-9]/ ) {a[$i]++;break}
}
END {
   for (i in a)
     print "Error Code : " i " Count : " a[i]
}
' file1.txt

# 5  
Old 07-10-2009
HI Gersh99,

Thank you again for the prompt reply and it working fine and working much faster to my code(which I mentioned in the 1st post).
Nawk is something new to me, searched in the beginners unix book (by wrox) nothing much available.

Please correct me if Im worng :
The code is taking 'space' as the space separator.
and if the file is somewhat like this :
Code:
sjhgfjhgdfs ora-0001 kjhsf 098j 97h
suiy23vb jhf8 ora-0001
jkhsdkj 98798 error:ora-0001 uif987

then am getting the output not as ora-0001 : 3 but as
Code:
ora-0001 : 2
error:ora-0001 : 1

is there anyway i can extract only words starting with ora-

Last edited by vgersh99; 07-10-2009 at 07:48 AM.. Reason: code tags, PLEASE!
# 6  
Old 07-10-2009
Please use CODE tags, am having difficulty in reading your sample input.
# 7  
Old 07-10-2009
Code:
nawk '
/ora-[0-9]/ {
    for(i=1;i<=NF;i++)
       if ( $i ~ /ora-[0-9]/ ) { a[substr($i,index($i,"ora-"))]++; break}
}
END {
   for (i in a)
     print "Error Code : " i " Count : " a[i]
}
' file1.txt


Last edited by vgersh99; 07-10-2009 at 07:56 AM.. Reason: ooops, sorry - misread the requirement
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

How to append word count at end of file?

Hi guys, I have to append the word count of a file at the end of the file name like this - > "filename_2345" where 2345 is the word count of "filename". How do i achieve this using one command ? I am not allowed to store the word count in a variable and then concatenate. Request your... (1 Reply)
Discussion started by: peter2312
1 Replies

3. UNIX for Dummies Questions & Answers

Taking word count from file and printing in file

hi, i am having a file which contains the below content, i need to take the word count of if and print the file name also inputfile.txt file_name1.txt,type_name1.txt file_name2.txt,type_name2.txt i would need the word count of the files like this if file_name*.txt then wc -l... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

4. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

5. Shell Programming and Scripting

Count characters in a csv file and add an word.

Hello, I want to add a sentence to "post column" those who are only less than 30 characters.Thank you very much for your help. "category","title","post" "Z","Zoo","test 54325 test 45363mc." "Z","Zen","rs2w3rsj 2d342dg 2d3s4f23 d23423s23h 2s34s2423g ds232d34 2342." "Z","Zet","test4444... (3 Replies)
Discussion started by: hoo
3 Replies

6. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

7. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

8. Shell Programming and Scripting

How to find a count of a word within a file

Hello, I'm looking for a wait to count the number of occurrences of a certain string of characters within a file. The file that I trying to parce has segments within the file that have a header and footer to each segment and I'm trying to do a count of the header string and compare it to a count... (9 Replies)
Discussion started by: bd_joy
9 Replies

9. Shell Programming and Scripting

grep all records in a file and get a word count -perl

Hi, I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script thanks (4 Replies)
Discussion started by: meghana
4 Replies

10. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies
Login or Register to Ask a Question