Computing.Net > Forums > Programming > Delete all but last 10 lines .txt

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Delete all but last 10 lines .txt

Reply to Message Icon

Name: CR
Date: December 12, 2008 at 16:19:11 Pacific
OS: Windows XP Pro SP3
CPU/Ram: AMD XP 2500+ 1GB
Product: Custom / CUSTOM BUILD
Comment:

Hi, I wonder if this can be done using a Windows batch file - I need to delete all but the last 10 lines from a log file created by my antivirus application.

I have worked out how to take the entire contents of the virus scan log and append it to a log I have already started by using the following:

COPY "%FILENAME%"+"Virus_Removal\ClamWinPortable\Data\log\ClamWinLog.txt" "%FILENAME%" > NUL

However, the log that is generated lists every single file that has been scanned then adds a summary at the bottom. What I need to do is remove the huge list of files scanned (which can be any number of lines of text) and just keep/export the summary or just pull the summary from the log file and append it to another text file - either would be cool :)

I.E.
I need to keep the bold section:

C:\Windo...... etc...
C:\WINDOWS\system32\IMM32.DLL: OK
C:\WINDOWS\system32\avgrsstx.dll: OK
C:\WINDOWS\system32\Apphelp.dll: OK
C:\WINDOWS\system32\MSGINA.dll: OK
C:\WINDOWS\system32\COMCTL32.dll: OK
C:\WINDOWS\system32\ODBC32.dll: OK
C:\WINDOWS\system32\comdlg32.dll: OK

Scanning aborted...

----------- SCAN SUMMARY -----------
Known viruses: 472319
Engine version: 0.94.1
Scanned directories: 0
Scanned files: 39
Infected files: 0
Data scanned: 16.50 MB

Hope you guys 'n' gals can help with this one.



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: December 12, 2008 at 16:35:37 Pacific
Reply:

FOR /F "tokens=* delims=" %%A in ("log.txt") DO (
SET a=%%A
IF NOT !a:~0,1!==C ECHO %%A>>Keeper.txt
)

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: CR
Date: December 12, 2008 at 16:39:35 Pacific
Reply:

Fantastic, thanks for your speedy repy. However, being a noob to batch scripting could you please advise how your code works?

And if poss how I can change the amount of log/lines at the end of the antivirus log to keep?

Thanks


0

Response Number 3
Name: BatchFreak
Date: December 12, 2008 at 19:38:15 Pacific
Reply:

That doesn't just keep your last 10 lines. I actually cheated a little :) It keeps any line that doesn't start with a C so with your example it will keep

Scanning aborted...

----------- SCAN SUMMARY -----------
Known viruses: 472319
Engine version: 0.94.1
Scanned directories: 0
Scanned files: 39
Infected files: 0
Data scanned: 16.50 MB

It is a FOR command... the first line sets the name of the text file to look through, as well as the variables name (%%A). The next two lines perform commands on it. First setting each line as a manipulatable variable (%a%) Then Parsing out all the lines that do not start with "C". It won't work if you use it on a drive that isn't C: Then you would need to change it a little.

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 4
Name: ghostdog
Date: December 12, 2008 at 20:11:00 Pacific
Reply:

if you have Windows services for Unix, you can use the tail command. eg


c:\> tail -10 file > newfile.txt

or you can download from here.


0

Response Number 5
Name: Mechanix2Go
Date: December 13, 2008 at 10:58:39 Pacific
Reply:

@echo off > newfile& setLocal EnableDelayedExpansion

set /p K=this keeps the lasr N lines : how many do you need?
if !K!'==' goto :eof

for /f "tokens=* delims= " %%a in ('find /v /c "" ^< myfile') do (
set total=%%a
)

set /a K=!total!-!K!

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! gtr !K! echo %%a >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Delete all but last 10 lines .txt

To get the last 10 lines of a file. www.computing.net/answers/programming/to-get-the-last-10-lines-of-a-file/20114.html

Delete 1st 4 lines & last 8 Lines in TXT File www.computing.net/answers/programming/delete-1st-4-lines-last-8-lines-in-txt-file/19676.html

VBScript to delete all subfolders www.computing.net/answers/programming/vbscript-to-delete-all-subfolders/14650.html