|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|