I have a batch script that need to check what a textfile (.log or.txt) contains.

When it's done checking, it needs to delete those lines that contain the same configuration of charachters that is defined in another variable.

So, if var1=abc then every line that has "abc" as the last (in this case, three characters) gets deleted from the text file.

How can i do that?



I would try findstr.exe with the /v option:

findstr /v "text_to_drop" file.txt

If you're using powershell, try:

PS C:\> Select-String -Path "process.txt" -Pattern "text_to_drop" -NotMatch

See https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490907(v=technet.10) and https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-6

  • 1
    You will need to use regular expressions to match a string at the end of line, eg findstr /v /r /c:"%var1%$" file.log. The output will need to be saved to another file, which is then moved to overwrite the original file.log. – AFH Oct 22 '18 at 21:53
  • 1
    @AFH - or use the /E option: findstr /v /b /c:"%var1%" file.log. Either way, it only matches lines that end with \r\n. If the last line is missing the line terminator, or if unix style lines are used, then this will not work. – dbenham Oct 23 '18 at 16:38
  • @dbenham - Yes, I forgot about /e, which is simpler than using regular expressions; it also won't be affected by RE reserved characters in the match string. – AFH Oct 23 '18 at 17:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.