LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2008, 11:28 AM   #1
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Script Help: How to count a matching pattern in one line?


Hey everyone,

I know how to use grep, sed and awk in its most basic form, but I need to know how can I use either one of these commands or another to perform this task.

I Know how to count matching lines by piping it to wc. I want to know how can I count a matching pattern on one line, for example:

Quote:
The big bear jumped over the big fence.
I would like to know how many times the word big appears on one line, in our case 2 times.

In the past, I've used xargs to put each word on a separate line, then piped it to grep and wc to get the result.

If you know a better way I can achieve the same result please let me know.

Thanks in advance

Last edited by dv502; 12-13-2008 at 11:33 AM.
 
Old 12-13-2008, 12:56 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Here is a method using awk. The gsub function returns the number of the substitution made, so you can simply do
Code:
echo "The big bear jumped over the big fence." | awk '{print gsub(/\ybig\y/,"")}'
The \y notation in the regular expression is the gawk-specific operator to match the word boundary. Use that if you want to exclude the pattern within other words and count only the word "big" as is.

Using the same method you can count the total number of a word in a file:
Code:
awk '{count += gsub(/\ybig\y/,"")} END{print count}' file

Last edited by colucix; 12-13-2008 at 12:59 PM.
 
Old 12-13-2008, 01:02 PM   #3
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
Code:
#!/bin/bash

COUNTER=0
LINE="The big bear jumped over the big fence"
for WORD in $LINE
do
    if [ "$WORD" = "big" ]
    then
        COUNTER=`expr $COUNTER + 1`
    fi
done

echo $COUNTER
 
Old 12-13-2008, 01:53 PM   #4
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Original Poster
Rep: Reputation: 57
Thanks colucix, I tried both of your suggested codes and they work great and thank you rizwanrafique for the mini script you provided. The codes provided by both of you has help me greatly.

Thanks again.

Last edited by dv502; 12-13-2008 at 01:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
omit white spaces for pattern matching in shell script bharatbsharma Programming 4 09-18-2008 08:25 PM
shell script - matching portion of the line alenD Programming 11 10-02-2007 10:22 PM
Linux/Unix script for file pattern matching varunnarang Programming 1 08-07-2006 01:14 PM
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration