The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 07-09-2009
unx100 unx100 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 19
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
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
-->
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 :
Error Code : ORA-0001 Count : 2 Error Code : ORA-0002 Count : 2 Error Code : ORA-0004 Count : 1 Error Code : ORA-0008 Count : 1 -->
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.

#!/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 -->
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!