Solved

Grep syntax

Posted on 2008-10-23
5
661 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 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…

710 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