Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Grep syntax

Posted on 2008-10-23
5
Medium Priority
?
666 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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 600 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 300 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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