简体   繁体   中英

How to use the TYPE command in a batch file to output only a part of the line

I have a batch file that outputs only a few lines from a log file. The command I'm using is the following:

TYPE "file.log" | FINDSTR /C:"CPU usage:" /C:"RAM:" | FINDSTR /v /c:"Total: 0 B/s" >> "temp.log"

I'm trying to speed up this command and reduce the size on the output file. To achieve that, I would like to output only a part of the line. A typical line looks like:

2   2015-01-20 18:07:16.808 INFO    somelongtext1   text2   shorttext3  Physical RAM: Total=8000 MB
3   2015-01-20 18:07:16.886 INFO    text5   shorttext2  text6       Total CPU usage: 42.87%

Is there a way to output the line without the text1, text2, textX.... ?

The second FINDSTR makes sure I don't output line with value "0".

The columns do not have a fixed length however the text I want to get ride of is almost always the same (3-4 different words).

My files have about 100000 lines and I will output about 10000 only

Well, firstly, you've got a useless use of TYPE . findstr is capable of reading text files. Your command can be made a bit more efficient by getting rid of TYPE . You can also gain some efficiency by replacing your findstr /v with internal substring substitution.

@echo off
setlocal enabledelayedexpansion

>"temp.log" (
    (
        for /f "tokens=1-4,7*" %%I in (
            'findstr /c:"CPU usage:" /c:"RAM:" "file.log"'
        ) do (
            set "line=%%I %%J %%K %%L %%N"
            if "!line!"=="!line: 0 B/s=!" (
                echo(!line!
            )
        )
    )
)

It's more code, but internal commands are usually faster than executables. The initial findstr will help if you're parsing large files, though. Text stream processors are faster than for loops.

It looks like your log you're scraping always needs columns 5, 6, and 7 removed. If that's the case, you can simply include a tokens argument in your for /f loop and keep columns 1 through 4, then everything after 7. (Skipping %%M was intentional in the code above.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM