Finding exact string in a line

Acer Aspire 5920g
August 6, 2010 at 00:27:44
Specs: Microsoft Windows 7 Professional, 2.201 GHz / 2046 MB
Hello guys i am having problem printing out the exact line in a string. I have tried using the /x parameter but its not working so i need some help...thanks!


Codes below
findstr /x "SeNetworkLogonRight=*S-1-1-0IUSR_SEANAdministrators" C:\user_rights.txt


See More: Finding exact string in a line

Report •

#1
August 6, 2010 at 00:36:32
Try:

findstr /x "SeNetworkLogonRight=\*S-1-1-0IUSR_SEANAdministrators" 

or

findstr /x /l "SeNetworkLogonRight=*S-1-1-0IUSR_SEANAdministrators" 

It's strange that a full wildcard (zero or more characters of anything) would cause the problem, but it seems to....


edit:
I just remembered that "." is the wild card and "*" is the repeat for findstr, so it makes perfect sense...


Report •

#2
August 6, 2010 at 02:55:15
hmm it doesnt work..i have problem with the /x operator. I have tried creating a simple text file containing the word "hello" inside.
But when i do findstr /x "hello" C:\hi.txt it doesnt show me anything..whats wrong man?

Report •

#3
August 6, 2010 at 05:08:53
"/x" does mean the whole line, including any spaces, tabs or any other character.....

Do you have absolutely anything else on the line?


Are there any other unexpected attributes to this file? (such as being a unix lf only vs a dos/windows crlf).


Other than that I tested with xp's findstr, it could have been changed with windows 7....


Report •

Related Solutions

#4
August 6, 2010 at 06:43:23
im doing this operation on a windows xp inside a virtual machine..i have tried it on windows 7 too but i cant get the /x operator to work.I am very sure i type the exact string..

Report •

#5
August 6, 2010 at 07:00:37
The two searches work fine for me(with a file name appended) so I'm not sure what to say......


Is it possible the text file is not ansi encoded, perhaps unicode? Findstr doesn't seem to like unicode, I haven't tried with other formats....

edit
Can you upload the text file somewhere like megaupload of mediafire?


Report •

#6
August 6, 2010 at 07:07:58
Okay, it called hi.txt containing the word computer inside...

http://www.mediafire.com/?ki8s86h2e...


Report •

#7
August 6, 2010 at 07:46:59
Thank you, that made it fairly plain what the problem is!


Findstr doesn't seem to like to to find lines with "/x"(or /e) if they don't end in a line break.


D:\Downloads>findstr /x "computer" hi.txt

D:\Downloads>findstr /b /e "computer" "hi.txt"

D:\Downloads>findstr /b "computer" "hi.txt"
computer
D:\Downloads>type hi.txt | findstr /x "computer"
computer

D:\Downloads>echo.>>hi.txt

D:\Downloads>findstr /x "computer" hi.txt
computer

D:\Downloads>findstr /b /e "computer" "hi.txt"
computer

D:\Downloads>

The solution is simple, either add a line break to the end of the file/line or use a command like type that will add one automatically.

type file.txt | findstr /x "search-string"

or

>> file.txt echo.
findstr /x "search-string" file.txt


Report •

#8
August 6, 2010 at 08:24:48
yay i tried type file.txt | findstr /x "search-string" and it works! Thanks alot dude thats some valuable information i learnt up there

Report •

#9
August 6, 2010 at 11:23:37
argh i just cant seem to retrieve the exact string out of this line in the file below..
i have tried using type C:\nospace.txt | findstr /x "SeBackupPrivilege=AdministratorsBackupOperators"

http://www.mediafire.com/?r6lblch37...


Report •

#10
August 6, 2010 at 12:37:29
It won't match because there's a trailing space on every
line in nospace.txt.


Report •

#11
August 6, 2010 at 18:38:00
findstr /x /c:"SeBackupPrivilege=AdministratorsBackupOperators "


Report •

#12
August 6, 2010 at 23:03:04
that works:) thanks..i have learnt a lot from you!

Report •

#13
August 7, 2010 at 04:29:34
Partly out of curiosity, what is it that generated that file
(nospace.txt)? If it's one of your batch scripts, you're
probably echoing lines like this:

echo sometext >> file

If you lose the space between 'sometext' and '>>'...

echo sometext>> file

you won't get the trailing spaces in the file which may
save you any frustration/nasty surprises in the future.



Report •

#14
August 7, 2010 at 04:45:44
@MarkLS
echo sometext>> file


That leaves an issue if the last character on the line is a number and the second last a space(mimicking stream redirection):

echo something no 2>> Somefile
>> somefile echo something no 2

The same can happen with %variables%.


Its safer to redirect before echo, but if you know exactly what your dealing with and it's not affected then it doesn't matter.


Report •

#15
August 7, 2010 at 06:44:39
That leaves an issue if the last character on the line is a number and the second last a space(mimicking stream redirection)

That's interesting. Hadn't considered that before - just
thought it was some kind of personal preference which
looked kind of confusing. (Some of you 'long-termers'
in here seem to really know your stuff!)

Thanks for the heads up!



Report •

#16
August 7, 2010 at 09:20:18
hey sorry to trouble you guys but can i ask is there anyway to filter out only the BUILTIN\Administrator line and its atttributes to a text file? The constraint is that the file permissions are dynamic so i couldn't number the lines and filter them out..

Sample file
http://www.mediafire.com/?2emuzt1kp...


Report •

#17
August 7, 2010 at 09:39:57
Obviously, the solution is to get better output.

Report •

#18
August 8, 2010 at 12:25:49
hey sorry to trouble you guys but can i ask
is there anyway to filter out only the
BUILTIN\Administrator line and its atttributes
to a text file?

You can try the code below. Not sure if it's ultimately what
you want though. Did you check out the link that Razor
posted in Response # 17? How does that change things?

EDIT: I've just noticed that, although the code works
here on the file that you uploaded,
if you edit it in the slightest way, it all goes
pear-shaped. Not sure what's going on there,
can't figure it out, but probably best that you
give it a miss :)

@echo off

setlocal EnableDelayedExpansion

set InFile=hi1.txt
set OutFile=out.txt

type nul> "%OutFile%"
for /f "tokens=1 delims=:" %%a in ('findstr /n /c:"BUILTIN\Administrators:(special access:)" "%InFile%"') do set DataStart=%%a
for /f "tokens=1 delims=:" %%a in ('findstr /r /n /c:"^[ ]*$" "%InFile%"') do (
  set DataEnd=%%a
  if !DataEnd! gtr %DataStart% goto LineNumbersDone
)
:LineNumbersDone

for /f "tokens=1* delims=:" %%a in ('findstr /r /n ".*" "%InFile%"') do (
  if %%a geq %DataStart% if %%a lss %DataEnd% echo %%b>> "%OutFile%"
)


The logic is to find the line numbers where the data
starts and ends, using a blank line or - as it is in this
case - a line with only spaces to mark the end of
the data, then print out the lines in that range.
Filenames are set to hi1.txt (which is what you
posted) for input and out.txt for the output.



Report •

#19
August 9, 2010 at 06:56:06
OK, I found more time to figure out what the probable
cause of the trouble was (re: my Response #18). The
file that you uploaded has a strange mixture of CR, CRLFs
for line breaks. You definitely need better output ;)



Report •

#20
August 20, 2010 at 20:08:36
Sorry for bumping this topic, but i just want to say i have used another alternative suggested by Razor...and thank you everyone for helping me!

Report •

Ask Question