LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-20-2009, 03:24 AM   #1
grunge_1
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
Need help in word count command


Hi,
I have a file(text.txt) and it something like this:

Book and Book book
I don't know
what to write here Book
I need to know what
Book

Ok, when do word count it should say the word 'book' appear 5 times. I try this command but no luck...!!

grep -i book text.txt |wc -w <--- result 10

cat text.txt | grep -i book |wc -w <--- result 10

cat text.txt | grep -i book |wc -l <--- result 3

grep -c book text.txt <--- result 1


Anyone can help?

Thanks.
 
Old 03-20-2009, 03:29 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,345

Rep: Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945
1 - number of words in all the lines which contain the word "book"

2 - same as 1

3 - number of lines containing the word "book"

4 - number of lines containing the word "book", case sensitive.

Last edited by acid_kewpie; 03-20-2009 at 03:45 AM.
 
Old 03-20-2009, 03:35 AM   #3
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Code:
grep -o book <file> | wc -l
Maybe there's a nicer way.
 
Old 03-20-2009, 03:36 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,801

Rep: Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923
Or perhaps more simply "grep -oi book file | wc -l"
 
Old 03-20-2009, 04:01 AM   #5
grunge_1
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks all..
"grep -oi book file | wc -l" working just great!!!
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
word count issue George2 Programming 6 11-27-2007 06:11 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 12:46 PM
word count pantera Programming 2 08-31-2004 07:23 AM
word count in a line pantera Programming 4 08-25-2004 01:14 PM


All times are GMT -5. The time now is 05:24 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration