how to find the exact pattern from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find the exact pattern from a file?
# 1  
Old 12-05-2005
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  
Old 12-05-2005
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  
Old 12-05-2005
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  
Old 12-05-2005
try this
grep "^pattern$" file |wc -w
# 5  
Old 12-05-2005
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  
Old 12-05-2005
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  
Old 12-05-2005
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To find the exact pattern

My Input : Hi editor this is the exact pattern which we looking for the previous patternmatch My code: awk '/pattern/ { print a } { a = $0 }' Current output : exact previous (3 Replies)
Discussion started by: Roozo
3 Replies

2. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

3. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

4. UNIX for Dummies Questions & Answers

How to find a file if we dont know exact location of file ?

Hi I want know "How to find a file if we dont know exact location of file ?" Thanks, Tushar Joshi:) (9 Replies)
Discussion started by: tusharjoshi
9 Replies

5. UNIX for Advanced & Expert Users

Find exact path of a file/directory

Hello Folks, A wrapper takes an argument of file or directory name. I want to allow paths that reside within the current directory only. Can simply discard the paths like "/A" & "../" as they go outside the current by looking at the path beginning. How to validate this one: A/../../../b... (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

6. Shell Programming and Scripting

Want to grep exact pattern from file

Contents of my file is: DI DI DIR PHI I want to extract only DI. I am using below command grep -w DI <file> but it is also extracting DI. Can i use any other command to extract exact pattern where '[' does not have special meaning (4 Replies)
Discussion started by: nehashine
4 Replies

7. Shell Programming and Scripting

Find file with exact access date

Hi I have to write command that find the files/dirs in the directory with access date equal to timestamp. ie or to be more precise I need to find files which are not equal to given timestamp drwxr-xr-x 2 oracle oinstall 4096 May 31 2007 tmp so need to have something like find . *... (2 Replies)
Discussion started by: zam
2 Replies

8. UNIX for Dummies Questions & Answers

How to find exact text in file ?

I have file named shortlist , and it contains this: 2233|charles harris |g.m. |sales |12/12/52| 90000 9876|bill johnson |director |production|03/12/50|130000 5678|robert dylan |d.g.m. |marketing |04/19/43| 85000 2365|john woodcock |director |personnel... (1 Reply)
Discussion started by: Cecko
1 Replies

9. Shell Programming and Scripting

Find Exact word in file

Hi ALL, I want to search one string “20 “ i.e 20 with space. But my file where I am searching this “20 “ contain some data like 120 before image file truncated 220 Reports section succeeded 20 Transaction database .prd stopped 220 Reports section completed. When I search for the... (5 Replies)
Discussion started by: Jeevan Salunke
5 Replies
Login or Register to Ask a Question