There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
audio avg avg 8 blue screen brand new codec control panel conversion crash delete personal data desktop display dos driver duplicate dvd error error message excel explorer file firefox game graphics hardware hijackthis log install installation internet itunes javascript laptop macro malware monitor msconfig msn music network outlook outlook 2003 outlook express php problem program random rundll32 security seo sound sp3 spyware switch tag cloud trojan usb video virtumonde virus vista visual basic vundo wallpaper windows windows vista windows xp wireless word xp sp3 youtube
DOS/PDA/Other
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Search a file with findstr


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
08-May-2008, 01:51 AM #1
Search a file with findstr
My next order of busines is to do a profanity search. I have a file with a list of profanity words in it. One word per line. Need to search a data file and output the lines that match any of the profanity words in my Profanity file to one file and the ones that don't match to another file. I think I could do that with a For, Type,echo and Findstr commands. Get the errorlevel of findstr and echo the line to the appropriate output file. Something like

for /f %%a in (type somefile.txt) do echo %%a | findstr /grofanitywords.txt | if errorlevel==0 echo %%a >> profanity_output.txt else echo %%a>>not_profanity.txt

Something like that. I know the syntax isn't correct. Just trying to convey what I am thinking in a more technical manner.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,299 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
08-May-2008, 04:25 AM #2
Try this:
Code:
@echo off
Set _WL=Profanitywords.txt
Set _OF1=profanity_output.txt
Set _OF2=Not_Profanity.txt
Set _SF=file 1.txt
If EXIST %_OF1% del %_OF1%
If EXIST %_OF2% del %_OF2%
For /f "usebackq tokens=*" %%a In ("%_SF%") Do (
   echo.%%a|findstr /g:"%_WL%">>%_OF1%
   echo.%%a|findstr /v /g:"%_WL%">>%_OF2%)
For %%A In (WL OF1 OF2 SF) Do Set _%%A=
Jerry
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,299 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
08-May-2008, 05:52 AM #3
This one is much faster, as it only does the findstr once per line

Code:
@echo off
Echo.%time%>time1.txt
setlocal enabledelayedexpansion
Set _WL=search.txt
Set _OF0=Profanity.txt
Set _OF1=Not_Profanity.txt
Set _SF=file 1.txt
If EXIST %_OF0% del %_OF0%
If EXIST %_OF1% del %_OF1%
For /f "usebackq tokens=*" %%a In ("%_SF%") Do (
  Set _T0=%%a & echo.%%a|findstr /g:"%_WL%">nul & call :_Output!errorlevel!
  )
Echo.%time%>>time1.txt
Goto:EOF
:_Output0
echo.!_T0!>>%_OF0%
Goto:EOF
:_Output1
echo.!_T0!>>%_OF1%
Goto:EOF
But your original idea using if then else is the fastest:
Code:
@echo off
setlocal enabledelayedexpansion
Set _WL=search.txt
Set _OF0=Profanity.txt
Set _OF1=Not_Profanity.txt
Set _SF=file 1.txt
If EXIST %_OF0% del %_OF0%
If EXIST %_OF1% del %_OF1%
For /f "usebackq tokens=*" %%a In ("%_SF%") Do (
  echo.%%a|findstr /g:"%_WL%">nul
  If !errorlevel!==0 (echo.%%a>>%_OF0%) else echo.%%a>>%_OF1%
  )
On a relative scale, times for the 1st, 2nd, and 3rd versions are:
  1. 1.000
  2. 0.556
  3. 0.500

Not too surprising that the first one takes twice as long, as it does the findstr twice.
Doesn't seem to make any difference if you use the /v switch on findstr.

Jerry
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
09-May-2008, 12:30 AM #4
I am think I should probably throw in a /I so it is not case sensitive.
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
09-May-2008, 12:37 AM #5
And I forgot to say Thanks again!!!!
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,299 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
09-May-2008, 01:30 AM #6
Quote:
Originally Posted by Squashman View Post
I am think I should probably throw in a /I so it is not case sensitive.
Good catch! Seems I did forget to do that.

And You're Welcome!

BTW, I did finally convert the other batch to an exe; didn't make a noticeable difference in speed, still 35 minutes. It was a freeware converter - a paid version might make a difference, but probably won't come close to beating the *nix utilities.
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
09-May-2008, 10:50 PM #7
I found a really nice bat2exe that is free. I took the batch file I wrote and it let me compile it with all my ported unix utilities. Glad you mentioned that. Makes things much easier.

Now, I justed need to see how long it will take to run your profanity suppression on something like 1 million lines.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User
devil_himself's Avatar
Distinguished Member with 4,589 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
10-May-2008, 12:46 AM #8
you can also do it like this
Code:
for /f %%a in (myfile.txt) do (find /i  "%%a" "Profanitywords.txt">nul)&& echo %%a>>pr.txt || echo %%a>>npr.txt
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
10-May-2008, 11:15 PM #9
Quote:
Originally Posted by devil_himself View Post
you can also do it like this
Code:
for /f %%a in (myfile.txt) do (find /i  "%%a" "Profanitywords.txt">nul)&& echo %%a>>pr.txt || echo %%a>>npr.txt
Thanks, I will test that out when I get back to work. Need to mock up a file with a couple million records to see what happens.
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
15-May-2008, 12:06 AM #10
Trying to use the ConGetFile Utility to prompt me for a filename. Can't get it to work.

Code:
@echo off
setlocal enabledelayedexpansion
Set _WL=search.txt
Set _OF0=Profanity.txt
Set _OF1=Not_Profanity.txt
If EXIST %_OF0% del %_OF0%
If EXIST %_OF1% del %_OF1%
For /f "usebackq tokens=*" %%a In ('ConGetFile') Do (
  set InFile=%%a
  echo.%InFile%|findstr /I /g:"%_WL%">nul
  If !errorlevel!==0 (echo.%%a>>%_OF0%) else echo.%%a>>%_OF1%
  )

Last edited by Squashman : 15-May-2008 12:11 AM.
devil_himself's Avatar
Distinguished Member with 4,589 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
15-May-2008, 12:20 AM #11
Code:
@for /f "tokens=*" %%a in ('ConGetFile') do set file=%%a
devil_himself's Avatar
Distinguished Member with 4,589 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
15-May-2008, 12:31 AM #12
oops .. in full code

Code:
@echo off
setlocal enabledelayedexpansion
Set _WL=search.txt
Set _OF0=Profanity.txt
Set _OF1=Not_Profanity.txt
If EXIST %_OF0% del %_OF0%
If EXIST %_OF1% del %_OF1%
for /f "tokens=*" %%a in ('ConGetFile') do (
  for /f "usebackq tokens=*" %%b in ("%%a") do (  
  echo.%%b|findstr /I /g:"%_WL%">nul
  If !errorlevel!==0 (echo.%%b>>%_OF0%) else echo.%%b>>%_OF1%
 )
)
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
15-May-2008, 12:43 AM #13
Nevermind. I got it.

Code:
@echo off
setlocal enabledelayedexpansion
Set _WL=search.txt
Set _OF0=Profanity.txt
Set _OF1=Not_Profanity.txt
If EXIST %_OF0% del %_OF0%
If EXIST %_OF1% del %_OF1%
FOR /F "tokens=*" %%A in ('ConGetFile.exe') Do Set _Input=%%A
For /f "usebackq tokens=*" %%a In ("%_Input%") Do (
  echo.%InFile%|findstr /I /g:"%_WL%">nul
  If !errorlevel!==0 (echo.%%a>>%_OF0%) else echo.%%a>>%_OF1%
  )
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,299 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
15-May-2008, 06:05 AM #14
You've already figured it out, but just to clarify, when using the usebackq option you have to use an Accent Grave (` {usually under tilde ~}) around the command instead of the Apostrophe/Single Quote (')
So, instead of this:
For /f "usebackq tokens=*" %%a In ('ConGetFile') Do
you have to use this:
For /f "usebackq tokens=*" %%a In (`ConGetFile`) Do

But the only time you need to use the usebackq option is if the IN part of the For command contains a file name with quotes.
Some examples:
  • For /f "tokens=*" %a In (copy of search.txt) Do @echo Variable=%a
    Gives an error:
    The system cannot find the file copy.
  • If you use quotes around the file name
    For /f "tokens=*" %a In ("copy of search.txt") Do @echo Variable=%a
    the output is:
    Variable=copy of search.txt
  • if you use this:
    For /f "usebackq tokens=*" %a In ("copy of search.txt") Do @echo Variable=%a
    The output will be the contents of the file "copy of search.txt"
    Variable=This is line one
    Variable=This is line two

Jerry
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Squashman's Avatar
Distinguished Member with 11,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
15-May-2008, 06:23 PM #15
Thanks again for all the great input!
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:30 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.