How to find a string but not as a substring

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bill b.
Posts: 2
Joined: 19 Nov 2010 11:25

How to find a string but not as a substring

#1 Post by bill b. » 19 Nov 2010 11:31

Hi,
I need to find a string that 's standalone -- i.e., not embedded in another string. For example, I'd like to find "project" but not find "projectile"

Is there a way to do this? With findstr, I tried

"/<project/>" but that returned no hits.

thanks for helping!
bill

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Re: How to find a string but not as a substring

#2 Post by SenHu » 19 Nov 2010 18:47

This requires regular expressions. For this, you need biterscripting in Windows. Download it from any free download website. Enter this command in biterscripting.


Code: Select all

stex -r -c "^,project,^" " project projectile myproject"


You will see the results. -r means regular expression. -c means do case-insensitive search.

For regular expressions, check out http://www.biterscripting.com/helppages/RE.html .

Let's say, you want to change the free-standing word 'project' in file "/path/to/file.aspx". Here are the commands to use.

Code: Select all

var string content 
# We will read the file content into string variable $content
cat "/path/to/file.aspx" > $content
# We read the content of file into $content
sal -r -c "^,project,^" "XYZ" $content
# We replaced the free-standing word 'project' with
# XYZ.
echo $content > "/path/to/file.aspx"
# We wrote the file back with updated content.



You can put this commands in a script file C:/Replace.txt and execute this command.


Code: Select all

script "C:/Replace.txt"


It woud do the same. Obviously, change "/path/to/file.aspx" to appropriate file path and file name. Emclose it in double quotes.

If you need more help, just post. This is a great website to get windows programming questions answered.


Hope this helps.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to find a string but not as a substring

#3 Post by !k » 20 Nov 2010 11:34

Code: Select all

findstr /r /c:"\<project\>" file.txt

bill b.
Posts: 2
Joined: 19 Nov 2010 11:25

Re: How to find a string but not as a substring

#4 Post by bill b. » 22 Nov 2010 08:53

Thank you both!

!k, I tried your approach, but it didin't work. I added test cases to my file, and the syntax you describe couldn't find any strings, let alone substrings.

If I put the word "project" in an html file finstr "\<project\>" couldn't find it.

I'm running DOS on Winserver 2003. Maybe it's the version I'm running.

Again, thanks to you both. I'm going to try -r for now.

Bill
:?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to find a string but not as a substring

#5 Post by !k » 22 Nov 2010 09:12

bill b.
Sample file, plz.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: How to find a string but not as a substring

#6 Post by orange_batch » 22 Nov 2010 14:11

Is it possible to search for " project ", " project" or "project "? The space characters can provide the uniqueness required for such a search.

Post Reply