0

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?

1 Answer 1

0

Tom,

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

3
  • 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, 2018 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, 2018 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, 2018 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .