Findstr输出格式 [英] Findstr output formatting

查看:254
本文介绍了Findstr输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多日志文件,我想从中提取特定的文本,并使用FINDSTR命令将其复制到一个新文件



例如:我有日志文件1.log,我需要提取所有以 N66666 开头的数字:

  22220473400664-> 473400664 
01210260222-> 01210260222-> N8888801210260222
01278448231-> 01278448231-> N6666601278448231
01286799109-> 01286799109-> N8888801286799109

我试过:

  findstr / s / i\   

但结果是复制了以 N6666 到results.txt,其中我只需要将数字复制到results.txt。

解决方案

  @ECHO OFF 
SETLOCAL
SETtargetstring = N66666
FOR / ftokens = 3delims => %% a IN('findstr / s / i%targetstring%。**。*')DO ECHO(%% a

GOTO:EOF



这可能是可用的 - 您提供的信息非常短。



你需要的数据是字符串中第二个> 字符后面的行;我没有将结果重定向到一个文件,因为这是一个微不足道的修改。 p>




根据更多信息:

 SET/ltargetstring = 0
SEToutput =%
SETtargetstring = N66666 targetstring%
:dtslp
SET / a ltargetstring + = 1
SEToutput =%output:〜1%
IF DEFINED输出GOTO dtslp
FOR / f delims =%% a IN('findstr / s / i%targetstring%。**。*')DO(
SETline = %% a
SETline =! line:> =!
SETline =!line:< =!
SETline =!line:=!
CALL:process!line!


GOTO:EOF
:process
SEToutput =%1
IF NOT DEFINED输出GOTO:EOF
CALL SET = %% output:〜0,%ltargetstring %%%
IF / i%output%neq%targetstring%GOTO miss
SEToutput =%1
CALL SEToutput = %% output:〜%ltargetstring %%%
ECHO(%output%

:miss
SHIFT
GOTO process

您提供的信息应该已经编辑到您的问题中,而不是以评论发布。



您尚未回答原始解决方案依赖于您所需的数据是在所需行中的第二个>字符之后的字符串中您已声明输出number是在N66666和下一个空格之间(或者,给定数据提供,行尾;没有例子,在超出所需字符串的行上有额外的数据)



所以还有很多猜测。



此过程从文件读取包含目标字符串的每一行,然后替换ech>和<并删除任何



然后通过将找到的每个子字符串与目标字符串进行匹配,使用标准分隔符处理结果字符串使用子字符串方法将初始化时计算的目标字符串的长度应用到 ltargetstring 中。



如果第一部分找到的字符串匹配目标,提取字符串的剩余部分,并且重复该过程直到没有更多的参数:process


I have many log files where i want to extract specific text from it and copy it to a new file with FINDSTR command,

E.g: I have log files called 1.log and I need to extract all the numbers that start with N66666 lines in the log files that looks like:

22220473400664->473400664
 01210260222->01210260222->N8888801210260222 
 01278448231->01278448231->N6666601278448231
 01286799109->01286799109->N8888801286799109

I tried:

findstr /s /i "\<N66666.*" *.* > results.txt

but the result was copying the whole lines starting with N6666 to results.txt, where I need the numbers only to be copied to results.txt.

解决方案

@ECHO OFF
SETLOCAL
SET "targetstring=N66666"
FOR /f "tokens=3delims=>" %%a IN ('findstr /s /i "%targetstring%.*" *.*') DO ECHO(%%a

GOTO :EOF

This might be usable - you are very short on information provided.

It relies on your required data being the in the string after the second > character in the lines required; I've not redirected the result to a file as that's a trivial modification.


In the light of further information:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetstring=N66666"
SET /a ltargetstring=0
SET "output=%targetstring%"
:dtslp
SET /a ltargetstring+=1
SET "output=%output:~1%"
IF DEFINED output GOTO dtslp
FOR /f "delims=" %%a IN ('findstr /s /i "%targetstring%.*" *.*') DO (
 SET "line=%%a"
 SET "line=!line:>= !"
 SET "line=!line:<= !"
 SET "line=!line:"=!"
 CALL :process !line!
)

GOTO :EOF
:process
SET "output=%1"
IF NOT DEFINED output GOTO :EOF 
CALL SET "output=%%output:~0,%ltargetstring%%%"
IF /i "%output%" neq "%targetstring%" GOTO miss
SET "output=%1"
CALL SET "output=%%output:~%ltargetstring%%%"
ECHO(%output%

:miss
SHIFT
GOTO process

The information you have provided should have been edited into your question, not posted as comments.

You have not replied to the point that the original solution relied on "your required data being the in the string after the second > character in the lines required" you've stated that the output "number" that is required is that between the "N66666" and the next space (or, given the data provided, end-of-line; there is no example where there is extra data on the line beyond the required string)

So there's still a lot of guesswork.

This process reads each line that contains the target string from the files, then replaces ech ">" and "<" in tose strings with a space and removes any "

The resultant string is then processed using standard separtors by matching each substring found to the target string using the substring methodology applying the length of the target string calculated on initialisation into ltargetstring.

If the first part of the string found matches the target, the remainder of the string is extracted and echoed and the process repeated until there are no more parameters to :process.

这篇关于Findstr输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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