LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-01-2008, 01:07 AM   #1
atul_sp
LQ Newbie
 
Registered: Jan 2008
Posts: 11

Rep: Reputation: 0
How to find occurance of word in one line


Please let me know way so that i can find the occurrence of word in one line i.e

"hello how r u hello u r fine hello i am fine hello ru fine hello i hello u hello..."

i want to find count of the occurrence of hello word in this line. I tried to use "wc" but no success.

Last edited by atul_sp; 07-01-2008 at 01:11 AM.
 
Old 07-01-2008, 01:58 AM   #2
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Rep: Reputation: 15
Hi,

Hope this could help.
Code:
hari@edir:~> cat hello.txt
hello how r u hello u r fine hello i am fine hello ru fine hello i hello u hello
hari@edir:~> cat hello.txt |tr " " "\n"|grep hello|wc -l
7
actually the funda is .. convert the spaces in a line into a newline and then grep for the word.. then count the occurances.

I hope there is a simpler and much logical method to achieve this. Experts ... expecting you to bring it to light.



Thanks
Sridhar
 
Old 07-01-2008, 02:00 AM   #3
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Rep: Reputation: 15
If it is a file containing multiple lines and the same word occurs at different lines and different count.. the following could be an idea


Code:
while read line; do echo $line|tr " " "\n"|grep hello|wc -l; done < hello.txt

Sridhar
 
Old 07-01-2008, 02:04 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 68
Code:
grep -o hello | wc -l
Read man grep about -o and man wc about -l..

Oh, and \< and \> in regular expression denote beginning/end of the word.

Last edited by raskin; 07-01-2008 at 02:07 AM.
 
Old 07-01-2008, 04:20 AM   #5
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Rep: Reputation: 15
Hi Raskin,

That was great.. good learning for me!!

Sridhar
 
Old 07-21-2008, 02:20 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Right on, raskin.

The "-o" option for grep (mnemonic: "Who was the 6th Marx brother?"), is the reason we have a short "My favorite flag." segment at the beginning of our Sat. HLUG (Houston, TX) meetings. The idea is to introduce lesser known, but highly useful, options for common *nix commands. It also serves as a brief, & hopefully painless, reminder of the power of the CLI.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 02:57 PM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 10:46 PM
how to strip first word then every other line from text? Mandra Programming 6 12-04-2007 09:44 PM
add word to each line of the file? ziggie216 Linux - General 4 04-25-2005 05:19 AM
word count in a line pantera Programming 4 08-25-2004 02:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:25 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration