LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-31-2009, 06:07 AM   #1
lothario
Member
 
Registered: Apr 2004
Posts: 340

Rep: Reputation: 30
exact pattern matching


I need to count the numbers of lines in a file that contain the exact pattern
"out"
including the double quotes. So I did
Code:
 echo "found   "  `cat ./log20090831.txt  |  grep "out"  |  wc -l`
But this also finds lines that contain patterns like "shout", "about", ...
How do I fine tune this script?
 
Old 08-31-2009, 06:13 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can try
Code:
grep -w -c \"out\"
The -w option matches the whole word only, -c counts the matching lines and the double quotes are escaped to let the shell interpret them literally. Note that -c count the matching lines, not the matching words: this means that if two or more strings "out" appear in the same line, they are counted as one.

Edit: to count the actual number of words you can split the file by putting each word on a different line. This can be easily accomplished by a small awk program and passing output to the grep command, for example:
Code:
awk -v OFS='\n' '$1=$1' ./log20090831.txt | grep -w -c \"out\"

Last edited by colucix; 08-31-2009 at 06:19 AM. Reason: Improved code
 
Old 08-31-2009, 06:14 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The problem is caused by the shell doing its job and processing "out" as a double quoted word which results in it becoming simply out. Adding single quotes around "out" makes the shell treat it verbatim and pass exactly "out" to grep.
Code:
 echo "found   "  `cat ./log20090831.txt  |  grep '"out"'  |  wc -l`
 
  


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
[SOLVED] Remove exact matching word using sed kushalkoolwal Programming 2 08-20-2009 06:15 PM
grep exact pattern kapila Linux - Newbie 11 08-06-2009 03:58 AM
how to match the exact pattern using grep utility vinaytp Linux - Newbie 3 05-11-2009 01:36 AM
Pattern Matching Aveltium Linux - Newbie 5 04-13-2009 12:14 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 12:05 PM


All times are GMT -5. The time now is 07:23 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