Tech Support Forum banner
Status
Not open for further replies.
1 - 11 of 11 Posts

· Registered
Joined
·
214 Posts
Discussion Starter · #1 ·
I have a txt file with alot of lines that I want to process. I would like to delete every line containing a specific phrase. Is there an utlility or can one make a bat file that would be able to do this?
 

· Registered
Joined
·
2,446 Posts
Elaborating previous poster's post: when you pick replace, in the first box type in the exact phrase you want to replace. If you just want to delete leave the second box blank.
 

· Microsoft MVP
Joined
·
3,341 Posts
Notepad will only delete the specific phrase, not the entire line containing the phrase.

This batch file will do it. Edit the 3 set lines as needed. The set phrase line must have the quotes shown in red; don't use ", <, >, %, or | in the search phrase. Some other punctuation might also cause incorrect results.
This is not case sensitive, it you need it to be case sensitive, remove the /I switch.
You can have the program delete the original and rename the new file if you wish, but best to test first to make sure it's doing what you want.
Code:
@Echo Off
Set _SourceFile=C:\Test1\test.txt
Set _NewFile=C:\Test1\testv2.txt
Set _Phrase="Don't want lines with this phrase"
>"%_NewFIle%" Findstr /I /V %_Phrase% "%_SourceFile%"
Edit: adding the red quotes changed the spacing, it should be spaced like this:

Set _Phrase="Don't want lines with this phrase"

HTH

Jerry
 

· Registered
Joined
·
214 Posts
Discussion Starter · #5 ·
Thanks TheOutcaste you seem to understand my need.
Still need to ask
What is the Echo? and why should it be off? How do I get the quotes in red?
If i don't want a new file how do I change the line?
Is the the search sensitive to these symbols [ ]?
 

· Microsoft MVP
Joined
·
3,341 Posts
Thanks TheOutcaste you seem to understand my need.
Still need to ask
What is the Echo? and why should it be off? How do I get the quotes in red?
If i don't want a new file how do I change the line?
Is the the search sensitive to these symbols [ ]?
Turning off echo means the commands won't be displayed in the Command Prompt window. No real need to see each command except during troubleshooting, errors are still displayed. You can leave that line out if you wish.
I just colored those two quotes in red to show that they must be included. The phrase you search for must be in quotes.
You can use [ or ]. parentheses () and braces **} are also OK.

This doesn't include the Echo Off, and will replace the original file with the new one. Just be sure the phrase you use is unique enough to only be found in the lines you want to remove, as there will be no "undo" unless you have a backup copy of the original:

Code:
Set _SourceFile=C:\Test1\test.txt
Set _NewFile=C:\Test1\testv2.txt
Set _Phrase="Don't want lines with this phrase"
>"%_NewFIle%" Findstr /I /V %_Phrase% "%_SourceFile%"
Del /F /Q "%_SourceFile%"
Ren "%_NewFIle%" "%_SourceFile%"
Jerry
 

· Registered
Joined
·
214 Posts
Discussion Starter · #8 ·
Out curiosity Undocked Windy how would an app like that work and how easy is it to do it?

I got the bat file to work somehow but noticed something.
I tried using this "phrase" [t1]. But the bat file seem to find every line with a t and/or a 1 and delete it. Meaning it doesn't seem to be "phrase" sensitive.
Am I doing something wrong?
 

· Banned
Joined
·
3,574 Posts
The convenience of having it as a Visual Basic program, is that it's ALL visual and easy to do. .BAT files are kind of.. old.. and.. personally I don't like them.

If you want, I could whip you up a VB program really quick - I could even make it more specific for you.
 

· Microsoft MVP
Joined
·
3,341 Posts
Am I doing something wrong?
No, my mistake. [t1] is seen as a regular expression (RegEx) meaning match either t or 1. So need to add the /L switch to force it to not use RegEx
I found a couple other bugs. My bad for not testing better.
It will search for and remove lines with any word in the "phrase", not just lines with that exact phrase.
And to keep the file I used the Move syntax but typed Ren instead of Move.
Easy fixes though, give this a try:
Code:
Set _SourceFile=C:\Test1\test.txt
Set _NewFile=C:\Test1\testv2.txt
Set _Phrase="Don't want lines with this phrase [t1]"
>"%_NewFIle%" Findstr /I /L /V /C:%_Phrase% "%_SourceFile%"
Move /Y "%_NewFile%" "%_SourceFile%"
Jerry
 

· Registered
Joined
·
214 Posts
Discussion Starter · #11 ·
It 's not so easy getting everything rght all the time but it seem to work and I appreciate it alot TheOutcaste, thanks.
But I'm still qurious of your stuff Undocked Windy. But what do I need... visual basic? Do xp already have that or is it something I need to download from microsoft? If I could make any demands it would be nice if it could have an open file option where I can select my file. Not really sure how limited you are in this Undocked Windy. If it not to time consuming for you I would be happy if you would do it for me.
 
1 - 11 of 11 Posts
Status
Not open for further replies.
Top