Solved

Grep syntax

Posted on 2008-10-23
5
656 Views
Last Modified: 2012-05-05
I have a table in a file (piped).  I wanted to count the occurrence of a particular word XXX.  How do I do that in shell?
0
Comment
Question by:cpeters5
5 Comments
 
LVL 3

Expert Comment

by:raminhos
ID: 22791274
Hi.

Try:

grep -c "word_to_find" file.txt

0
 

Author Comment

by:cpeters5
ID: 22791725
raminhos:

Would this miss the case where there are more than one matches?
0
 
LVL 11

Accepted Solution

by:
jgiordano earned 200 total points
ID: 22791796
#!/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}
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 200 total points
ID: 22792013
grep -c would count lines with that word, no matter how many words found on that line.



# egrep -o pattern file|wc -l

Open in new window

0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 100 total points
ID: 22798035
if you want exact match add -w option and if you want to ignore case add -i option
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now