The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 09:10 AM
perl - how do i find out if a file doesn't contain a pattern? mjays Shell Programming and Scripting 4 09-19-2007 06:28 AM
find and replace a pattern in a file krishnamaraju Shell Programming and Scripting 1 08-29-2006 10:02 AM
How to find out the exact year in "Last modified time" using ls command Dophlinne UNIX for Dummies Questions & Answers 6 04-11-2006 11:07 AM
Find script with input pattern file iguanathompson Shell Programming and Scripting 8 02-06-2006 06:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-05-2005
surjyap surjyap is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 40
how to find the exact pattern from a file?

Hi
Let say there is a file a.txt which contain number rows.
My intention is to find the number of occurences of a pattern. Let say the pattern is mdbase. then it should not count the occurences of mdbase1 or mdbase2 like this. When I tried to find it like
grep "/backup/surjya/mdbase" xmldir.conf_backup | wc -w
The out put is 3 rather than 1. So please let me know how it can be resolved

Thanks
  #2 (permalink)  
Old 12-05-2005
grasper grasper is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 45
If mdbase is the end of the field or word separated from whatever follows by whitespace you can use:-

grep "/backup/surjya/mdbase\>" xmldir.conf_backup | wc -w
  #3 (permalink)  
Old 12-05-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by grasper
If mdbase is the end of the field or word separated from whatever follows by whitespace you can use:-

grep "/backup/surjya/mdbase\>" xmldir.conf_backup | wc -w

Without the wc

Code:
grep -c "/backup/surjya/mdbase\>" xmldir.conf_backup
  #4 (permalink)  
Old 12-05-2005
Raom Raom is offline
Registered User
  
 

Join Date: Sep 2005
Location: india
Posts: 79
try this
grep "^pattern$" file |wc -w
  #5 (permalink)  
Old 12-05-2005
surjyap surjyap is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 40
I tried like below:
dir2=\"$dir1"\>"\"
echo $dir2
stgdircount=`grep $dir2 xmldir.conf | wc -w | awk '{print $1}'`
echo $stgdircount


let say dir1=/backup/surjya/mdbase. hence the output for dir2 is
"/backup/surjya/mdbase\>"
and for stgdircount is 0, but exactly there is two entries in this file. Please give me some idea.
  #6 (permalink)  
Old 12-05-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
In the grep statment you gave, it is searching for

"/backup/surjya/mdbase\>"

I am sure you want to search for

/backup/surjya/mdbase and not the above statement.

Look at this

Code:
sh-2.05b$ cat surya.txt 
/backup/surjya/mdbasewed
/backup/surjya/mdbase2
/backup/surjya/mdbase
/backup/surjya/mdbase3
/backup/surjya/mdbase

sh-2.05b$ grep -c "/backup/surjya/mdbase\>" surya.txt 
2
sh-2.05b$ grep -c "/backup/surjya/mdbase" surya.txt 
5
  #7 (permalink)  
Old 12-05-2005
surjyap surjyap is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 40
yes you are right but why it gives 0 when I try to execute it through shell script. Let say the script is
dir1=/backup/surjya/mdbase
stgcnt=`grep -c $dir1 surya.txt `
echo $stgcnt

The value of stgcnt is 5.
Just modify the script like

dir1=/backup/surjya/mdbase
stgcnt=`grep -c \"$dir1"\>"\" surya.txt `
echo $stgcnt

The value of stgcnt is 0
I hope my question is clear.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0