批处理脚本从每个日志文件中提取相同的行 [英] Batch script to extract same line from each log file

查看:107
本文介绍了批处理脚本从每个日志文件中提取相同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多是总是相同的行数的文本文件 - 42和总是相同类型的每一行上的信息。同时每行开头的标题。

I have a number of text files that are always the same number of lines - 42 and always the same type of information on each line. Also each line starts with a header.

我想是一个批处理脚本保持文本文件的行删除30他人并保存文件。

What I would like is a batch script to keep line 30 of the text file remove the others and save the file.

我试图找一下基于两线之间的信息就行了。在这种情况下,上线30(作业注释)的标题和行标题31(职位编号),然后将信息写入到一个新文件。

I have tried to look find the line based on the information between two line. In this case the heading on line 30 (Job Notes) and the heading on line 31 (Job Number) and then write the information to a new file.

第30行始于

Job Notes= (information specifically about the job)

第31行始于

Job Number=

这是code我用(我发现本网站上其他地方),我根本没有得到输出。是否尝试过其他方面也这样真的没有使用此方法,如果你能看到一个更好的,基本上我只是想30行是文件中的唯一信息。

This is the code I used (which i found elsewhere on this site) and i am getting no output at all. Have tried other ways as well so don't really have to use this method if you can see a better one, basically i just want line 30 to be the only information in the file.

@ECHO OFF
SETLOCAL 
SET "sourcedir=C\Batch"
SET "destdir=C:\Batch\Extract"
for /f "tokens=1 delims=[]" %%a in ('find /n "Job Notes"^<"%sourcedir%\7099.txt" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "Job Number"^<"%sourcedir%\7099.txt" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%sourcedir%\00007099.txt" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
 )
)>"%destdir%\newfile.txt"
GOTO :EOF

任何帮助将大大AP preciated。
大卫

Any help would be greatly appreciated. David

推荐答案

选项1,使用FINDSTR串记数

Option 1, using findstr string numeration

for %%a in (*.log) do (
    for /f "tokens=1,* delims=:" %%b in (
        'findstr /n "^" "%%a" ^| findstr /b "30:"'
    ) do (
        echo(%%c>"%%a"
    )
)

选项2,使用命令行跳过

Option 2, using for command skip lines

setlocal disabledelayedexpansion
for %%a in (*.log) do (
    set "line="
    for /f "usebackq skip=29 delims=" %%b in ("%%a") do if not defined line set "line=%%b"
    setlocal enabledelayedexpansion
    echo(!line!>"%%a"
    endlocal
)

这篇关于批处理脚本从每个日志文件中提取相同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆