Findstr: search for strings ends with space or a newline

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Findstr: search for strings ends with space or a newline

#1 Post by sambasiva » 21 Jan 2014 10:09

Hi Experts,

I have some tough time with findstr for few days.

My requirement is to search for a particular string (from the output of net share command) which either ends with a space or a end of line.

For ex:
C:\>net share

Share name Resource Remark

-------------------------------------------------------------------------------
IPC$ Remote IPC
D$ D:\ Default share
C$ C:\ Default share
ADMIN$ C:\windows Remote Admin
ab C:\ab
abc C:\abc
abcdddafdfdd C:\abcdddafdfdd
abdafdafdsfdfadfdfdafdafdsfdfsafdsfdfdfdfdafdafdafdfdafdfdafdafdfdf
C:\abdafdafdsfdfadfdfdafdafdsfdfsafdsfdfdfdfdafdafdafdfdafdfdafdafdfdf


Here I wanted to search for the line which contains only "C:\ab".
However the below findstr command gives all matches with C:\ab.


C:\>FOR /F "tokens=*" %I IN ('net share ^|findstr /C:"C:\ab"' ) DO @echo %I
ab C:\ab
abc C:\abc
abcdddafdfdd C:\abcdddafdfdd
C:\abdafdafdsfdfadfdfdafdafdsfdfsafdsfdfdfdfdafdafdafdfdafdfdafdafdfdf


My requirement is to find the string "C:\ab" which either ends with a space or a end-of the line character.

Appreciate any help on this.


Thanks
Sambasiva

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Findstr: search for strings ends with space or a newline

#2 Post by penpen » 21 Jan 2014 10:32

If you need to specify a space or line end in the findstr search string, then just append a space or a line end (see: help findstr):

Code: Select all

findstr /R /C:"C:\ab " /C:"C:\ab$"

penpen

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: Findstr: search for strings ends with space or a newline

#3 Post by sambasiva » 21 Jan 2014 10:57

PenPen,

Thanks for the reply.

It didnt work in my case.

Code: Select all

C:\>net share

Share name   Resource                        Remark

-------------------------------------------------------------------------------
IPC$                                         Remote IPC
D$           D:\                             Default share
C$           C:\                             Default share
ADMIN$       C:\windows                      Remote Admin
ab           C:\ab
abc          C:\abc
abcdddafdfdd C:\abcdddafdfdd
abdafdafdsfdfadfdfdafdafdsfdfsafdsfdfdfdfdafdafdafdfdafdfdafdafdfdf
             C:\abdafdafdsfdfadfdfdafdafdsfdfsafdsfdfdfdfdafdafdafdfdafdfdafdafdfdf

The command completed successfully.



Below command didn't yield any output :(

C:\>net share |findstr /R /C:"C:\ab " /C:"C:\ab$"

C:\>

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Findstr: search for strings ends with space or a newline

#4 Post by penpen » 21 Jan 2014 11:14

Sorry, just a flaw with the '\' (why? see: "findstr /?"), use:

Code: Select all

net share |findstr /R /C:"C:\\ab " /C:"C:\\ab$"

penpen

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: Findstr: search for strings ends with space or a newline

#5 Post by sambasiva » 22 Jan 2014 06:20

It's working..Thanks a lot PenPen

Post Reply