![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| word count wc | chaandana | UNIX for Dummies Questions & Answers | 5 | 05-05-2009 11:47 AM |
| How to count the occurences of a specific word in a file in bash shell | mskart | UNIX for Dummies Questions & Answers | 2 | 10-30-2008 12:42 PM |
| How to find a count of a word within a file | bd_joy | Shell Programming and Scripting | 9 | 07-14-2008 10:29 AM |
| grep all records in a file and get a word count -perl | meghana | Shell Programming and Scripting | 4 | 02-13-2008 10:06 PM |
| Count the number of occurence of perticular word from file | rinku | Shell Programming and Scripting | 40 | 08-10-2007 08:33 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 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 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! |
|
||||
|
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! |
|
||||
|
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 Code:
ora-0001 : 2 error:ora-0001 : 1 Last edited by vgersh99; 07-10-2009 at 07:48 AM.. Reason: code tags, PLEASE! |
|
|||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|