【问题标题】:How to output certain lines from a batch script result to a file如何将批处理脚本结果中的某些行输出到文件
【发布时间】:2018-07-13 05:17:11
【问题描述】:

我只想将命令结果中的某些行输出到另一个文件中。
这是我的脚本

@echo off
del "C:\Users\admin\Desktop\script1results.txt"

cd C:\Users\ndujakov\DIG
for /f %%a in (C:\Users\admin\Desktop\script1inputs.txt) do (
dig %%a >> C:\Users\admin\Desktop\script1results.txt
) 

这是结果和我想具体输出的内容

; <<>> DiG 9.10.6-P1 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6710
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;google.com.            IN  A

;; ANSWER SECTION:
google.com.     248 IN  A   172.217.160.206    <---This one

;; Query time: 1 msec                          <---This one
;; SERVER: 10.231.0.106#53(10.231.0.106)
;; WHEN: Fri Feb 02 08:25:55 Pacific Standard Time 2018
;; MSG SIZE  rcvd: 55

【问题讨论】:

  • 如何决定,哪几行?
  • 打开命令提示符窗口并运行 findstr /? 以获取有关此命令的帮助。您可以指定多个搜索字符串,这些字符串被或解释为将过滤后的输出写入文件。

标签: batch-file scripting


【解决方案1】:

您可以开始使用此批处理文件来过滤您的结果:

@echo off
set "Inputfile=result.txt"
set "TmpFile=%Tmp%\%random%.txt"
set "OutPutFile=FilterResult.txt"
(FindStr /IR /C:"[0-9].[0-9].[0-9].[0-9]" /C:"msec" "%inputfile%")>"%TmpFile%"
(Findstr /IV "server" "%TmpFile%")>"%OutPutFile%"
Start "" "%OutPutFile%" & del "%TmpFile%"

进一步阅读

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2015-08-31
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    相关资源
    最近更新 更多