Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-14-2012
Registered User
 
Join Date: Nov 2008
Location: Bangalore, India
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
How to grep the exact string / word ?

Hi All,
I have a text / log file which contains strings like meta777, 77, meta, 777. Now I want to write a script which can detect a string 'meta#777' in a text file & number of occurence of 'meta', number of #, number 7, 77, 777.
I'm using grep -e '77' filename but no luck. It is returning string with 777 also. How to get the exact string. Please help me. Thanks in Advance.

Below are some lines from that file-


Code:
04/05/2012 7:21 PM    Attempting to Connect to DB: (local)
04/05/2012 7:21 PM    DB Connection Established: (local)
04/05/2012 7:21 PM    Getting Database Lists meta#7
04/05/2012 7:21 PM    
04/05/2012 7:21 PM    -----------------------------------------------------
04/05/2012 7:21 PM meta#777                Beginning Script Processing
04/05/2012 7:21 PM    -----------------------------------------------------
04/05/2012 7:21 PM    Clinical DB: Medicos
04/05/2012 7:21 PM    IDwf DB: IDXwf
04/05/2012 7:21 PM    Impact DB: Impact: meta#777
04/05/2012 7:21 PM    General DB: ASCharge, meta#777
04/05/2012 7:21 PM    -----------------------------------------------------
04/05/2012 7:21 PM    
04/05/2012 7:21 PM    Beginning Script Processing 777
04/05/2012 7:21 PM    IDXwf - Processing: 002-TWMenus-SP.sql ............. meta#77
04/05/2012 7:21 PM    IDf - Processing: 100-Base.sql
04/05/2012 7:21 PM    IDXwf - Processing: 204 - PHB_10_0_Upgrade.sql
04/05/2012 7:21 PM    IDXwf - Processing: 910-BuildMenus.sql
04/05/2012 7:21 PM    IDXwf - Processing: dbo.FileWF.prc
04/05/2012 7:21 PM    Successfully Finished Script Processing meta#777
04/05/2012 7:21 PM    
04/05/2012 7:21 PM    -----------------------------------------------------
04/05/2012 77:21 PM          meta#777End Script Processing 77
04/05/2012 777:21 PM    -----------------------------------------------------meta#777
04/05/2012 7:21 PM    
04/05/2012 7:21 PM    Attempting to Connect to DB: (local)meta
04/05/2012 7:21 PM    DB Connection Established: (local)meta#777
04/05/2012 7:21 PM    Getting Database Lists 777
04/05/2012 7:21 PM    -----------------------------------------------------
meta#777


Last edited by Scrutinizer; 06-15-2012 at 12:43 AM.. Reason: code tags
Sponsored Links
    #2  
Old 06-15-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
sed

Hi,

Try this one,

Code:
sed -n '/meta#/p;s/.*\(meta#[7][7]*\).*/\1/g' file

Cheers,
Ranga:-)

Last edited by rangarasan; 06-15-2012 at 01:58 AM..
The Following User Says Thank You to rangarasan For This Useful Post:
adc22 (06-15-2012)
Sponsored Links
    #3  
Old 06-15-2012
Registered User
 
Join Date: Nov 2008
Location: Bangalore, India
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks Rangarasan for the help. I tried with the solution that you provided. But I got all the matching lines, -


Code:
$ sed -n '/meta#/p;s/.*\(meta#[7][7]*\).*/\1/g' expression.txt
04/05/2012 7:21 PM      Getting Database Lists meta#7
04/05/2012 7:21 PM meta#777                 Beginning Script Processing
04/05/2012 7:21 PM      Impact DB: Impact: meta#777
04/05/2012 7:21 PM      General DB: ASCharge, meta#777
04/05/2012 7:21 PM      IDXwf - Processing: 002-TWMenus-SP.sql ............. meta#77
04/05/2012 7:21 PM      Successfully Finished Script Processing meta#777
04/05/2012 77:21 PM           meta#777End Script Processing 77
04/05/2012 777:21 PM    -----------------------------------------------------meta#777
04/05/2012 7:21 PM      DB Connection Established: (local)meta#777
meta#777
meta#777  this is test file meta#777

I'm using CYGWIN_NT-6.1. I'll later try this into my Ubuntu box.

Last edited by Scott; 06-15-2012 at 03:29 AM.. Reason: Code tags, please...
    #4  
Old 06-15-2012
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Location: istanbul
Posts: 1,629
Thanks: 1
Thanked 279 Times in 272 Posts

Code:
# awk 'BEGIN{x="meta#";s=7}{if($0~x){if($0~x s"$")sc++;if($0~x s s"$")ssc++;if($0~x s s s"$")sssc++}}
END{print x " counts -> " sc"(" x s")",ssc"(" x s s")",sssc"(" x s s s")"}' infile
meta# counts -> 1(meta#7) 1(meta#77) 6(meta#777)

The Following User Says Thank You to ygemici For This Useful Post:
adc22 (06-15-2012)
Sponsored Links
    #5  
Old 06-15-2012
Registered User
 
Join Date: Nov 2008
Location: Bangalore, India
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks Ygemici, it worked like magic .
I'm just trying to understand the code. Could you please explain me what
if($0~x){if($0~x s"$") actually does .
Thanks.
Sponsored Links
    #6  
Old 06-15-2012
Registered User
 
Join Date: Apr 2011
Posts: 7
Thanks: 12
Thanked 1 Time in 1 Post
Can't we use "grep -w" ? as per my experience 10 examples of grep command in UNIX and Linux it matches whole word , so my understanding is that it should match exact word like "77" and not "777" , also we can use wc -l to count lines , this is what I did as quick test
$ cat abc
77
7777
777
77777


$ grep -w 77 abc | wc -l
1


$ grep -w 777 abc | wc -l
1
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed thibodc Shell Programming and Scripting 1 05-23-2012 11:14 PM
grep exact string from a file vchee Shell Programming and Scripting 6 11-08-2011 01:30 AM
How to use grep to get an exact string? liuzhencc Shell Programming and Scripting 4 11-29-2010 01:34 AM
exact string match in a word dr_sabz UNIX for Dummies Questions & Answers 11 12-15-2008 05:00 AM
option of grep for counting exact word ?? maddy Linux 3 06-17-2008 08:55 AM



All times are GMT -4. The time now is 03:57 PM.