![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to find a count of a word within a file | bd_joy | Shell Programming and Scripting | 9 | 07-14-2008 06:29 AM |
| grep all records in a file and get a word count -perl | meghana | Shell Programming and Scripting | 4 | 02-13-2008 07:06 PM |
| Count number of occurences of a word | shikhakaul | UNIX for Dummies Questions & Answers | 7 | 11-30-2007 05:22 PM |
| search& count for the occurence of a word | skoppana | UNIX for Dummies Questions & Answers | 1 | 11-09-2007 02:07 PM |
| word count showing wrong number of lines | tselvanin | UNIX for Dummies Questions & Answers | 3 | 01-06-2004 08:33 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
The 'less' command is used to view a file. Use grep to search for a particular word in a file. You can use this to count the number of occurrences too, just check the man page for the exact switch.
|
|
#3
|
|||
|
|||
|
grep -o <string> <file> | wc -w
|
|
#4
|
||||
|
||||
|
Quote:
|
|
#5
|
|||
|
|||
|
Quote:
grep: illegal option -- o What does "-o" option do? I also need to find an occurence of a certain string within a file. Currently I'm using: Code:
grep -c 'abc' sample.txt Example: This is line 1 abc and abc This is line 2 abc |
|
#6
|
||||
|
||||
|
Use:
Code:
awk '{
for (i=1;i<=NF;i++)
if ( $i == "abc")
c++
}
END{
print c}' sample.txt
Code:
awk '
BEGIN {
RS=FS
}
{
if ( $0 ~ /abc/ )
c++
}
END{
print c++
}' lsample.txt
|
|
#7
|
||||
|
||||
|
This solution does not work.
Here is a sample file: Code:
a aa aaa aaa aa a aaa aa a aaa aa a aaa Code:
tr -cs 'A-Za-z' '\n' < FILE | grep -c "aaa" Here is another possible solution for those who want to use shell script: Code:
#!/bin/ksh
typeset -i mCnt=0
mWord='aaa'
for mEach in `cat input_file`
do
if [ "${mEach}" = "${mWord}" ]; then
mCnt=${mCnt}+1
fi
done
echo 'Total words for '${mWord}' = '${mCnt}
Last edited by Shell_Life; 08-08-2007 at 08:14 AM. |
||||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|