-1

How to remove a line on txt named "0 Files Copied" and the line below it (with the Date and Time)

Basically it removes the trash on a log file.

There is the edit:

findstr /v /i /c:"0 File(s)" Backuper_log.txt > Backuper_log2.txt
type Backuper_log2.txt > Backuper_log.txt
del Backuper_log2.txt
exit
  • 1
    What have you tried so far? We are not a free code generating community. First show some effort! – MichaelS Sep 17 '15 at 09:31
  • Ok sorry... It was not with evil intent. – Hubert Kenobi Sep 17 '15 at 09:40
  • 1
    How should other users know what you expect the code to do? please provide samples of the input and the output file; and remove tag [tag:ms-dos] as it does not apply (I'm pretty sure you're working on Windows); – aschipfl Sep 17 '15 at 09:47

1 Answers1

1

You can use FIND command with /V parameter, e.g.

find /V "0 Files Copied" log.txt > log2.txt

But this will only remove the lines with "0 Files Copied"; this command wouldn't allow you to remove the line which follows.

You can also use FINDSTR, which allows to search for regualr expressions.

findstr /V /R /C:"^0 Files Copied$" log.txt > log2.txt

You can also use findstr to independently remove lines with dates using an appropriate regular expression.

There is a way to search across multiple lines with findstr, but this method doesn't work well with /V option, as only the 1st line of the match is filtered. So you cannot use findstr to remove both the line which matches and the following line.

If you do need to do specifically that, I think your best bet is to use some alternative of grep for Windows.

You can also use Powershell with the following simple script. Just copy and paste it in the powershell command line, and press Enter twice to start execution. Make sure that input file is in your current folder.

$Lines = Get-Content "input.log"
for ($num = 0; $num -lt $lines.Count; $num++ ) {
    if ($Lines[$num] -match "0 Files Copied") {
        $num++; #skip next line
        continue
    }
    if ($num -match 0) {
        $Lines[$num] > "output.log" }
    else {
        $Lines[$num] >> "output.log" }
}
Community
  • 1
  • 1
il--ya
  • 315
  • 3
  • 7
  • So it's not possible to detect the next line of some "line". But if it detect some date equal to the upline? Something like : 2015-09-17 9:42:05,10 (line below:) 2015-09-17 9:42:06,35 – Hubert Kenobi Sep 17 '15 at 15:18
  • 1
    I don't think it's possible with findstr, but I've updated my answer to show how to do what you want in Powershell (which is supplied with win7 and later). – il--ya Sep 25 '15 at 20:23
  • Man there is a problem... I tried it on Powershell and I made a test with 5 lines and it gives me just one: http://postimg.org/image/4z88axx5f/ – Hubert Kenobi Oct 22 '15 at 00:38
  • In your sample, the exact string is "0 File(s) Copied" with parentheses around s.I don't quite understand which 5 lines you used for a test, – il--ya Dec 01 '16 at 18:09