Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching files for patterns

Status
Not open for further replies.

amacbean

Programmer
Nov 18, 2002
9
US
What is the best way to find all the occurances of a string pattern in a file?

I need to find all occurances of the pattern:

'<name>*</name>'

where the * could be any string.

Can this be done using grep?

 
Yes, but (as above) enclose your expression in single quotes, eg

grep '<name>*<name>' file

Cheers.
 
Thanks but I have tried that.

grep '<name>*</name>' file.txt - this does not work??
grep '<name>*' file.txt - this does!!

Any ideas why?
 


amacbean

Try this .... It should work :)

grep &quot;<name>.*</\name>&quot; file.txt

\n needs to be escaped
AND
When you use an asterisk in a regular expression
it will match any number of the character that
immeadiately precedes it. So ...
grep &quot;<name>*</\name>&quot;

Will match any number of > in between <name>
and <\name>, but . matches any single character
so .* will match any number of any charcter between
<name> and <\name>

Hope what I stated is true and hope it makes sense

JRjr
 


amacbean

Sorry I noticed I switched the slash around :(
</name> to <\name>
All you need to do is

grep &quot;<name>.*</name>&quot; file.txt

Single or double quotes work ..

JRjr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top