Solved

Grep syntax

Posted on 2008-10-23
5
662 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

617 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