如何修改Windows批处理脚本跳过Logging.csv前两行对比主机名
批处理脚本修改:跳过Logging.csv前两行匹配主机名
原脚本通过findstr直接检索整个Logging.csv文件,要实现跳过前两行的需求,可以借助more +2命令先过滤掉Logging.csv的前两行,再将剩余内容传给findstr进行匹配。
修改后的完整脚本如下:
@echo off setlocal enabledelayedexpansion for /f "skip=1" %%e in (hostname.csv) do ( type logging.csv | more +2 | findstr /i "%%e" >nul if errorlevel 1 ( echo !date! !time!: Node %%e has NOT reported in the last 24 hours >> missing.txt echo !date! !time!: Node %%e has NOT reported in the last 24 hours ) else ( echo !date! !time!: Node %%e reported in the last 24 hours >> found.txt ) ) endlocal
关键改动说明:
- 新增
setlocal enabledelayedexpansion和endlocal,将%date%、%time%改为!date!、!time!,解决原脚本循环中日期时间值不更新的隐患 - 用
type logging.csv | more +2 | findstr /i "%%e"替代原有的findstr /i "%%e" logging.csv:more +2会跳过Logging.csv的前两行,仅输出第三行及以后的内容- 通过管道将过滤后的内容传给
findstr,确保只在有效日志行中匹配主机名
- 移除原脚本中多余的
echo ... | echo ...写法,直接拆分两行输出即可
内容的提问来源于stack exchange,提问作者earthworm
相关产品推荐
相关产品推荐

