恐怕我无法想象如何编写正确的批处理脚本来解决以下任务。 你能帮忙吗?
我有一个文本文件“2024-04-29_22-23log.txt”,其中每一行(第一行除外)以 OPEN 或 CLOSE 开头,后跟时间戳。 为了突出显示状态更改,我只想保留该单词与上一行不同的行。 但我想保留前两行和最后一行。
示例: 1 2024-04-29_22-23 2 关闭 22:23 3 关闭 22:24 4 开放时间 22:25 5 开放时间 22:26 6 开盘 22:27 7 关闭 22:28 8 关闭 22:29 9 关闭 22:30
应该得到: 1 2024-04-29_22-23 2 关闭 22:23 3 开放时间 22:25 4 关闭 22:28 5 22:30 关闭
感谢您的帮助。
以下仅适用于按字母顺序排列的文件名,并且忘记了最后一行!
@echo off
setlocal enabledelayedexpansion
set /p "inputFile=Enter file name : "
set "outputFile=!inputFile!_filtered.txt"
rem Copy the first line to the output file
set "lineNum=0"
for /f "usebackq delims=" %%a in ("%inputFile%") do (
set /a "lineNum+=1"
if !lineNum! equ 1 (
echo %%a>>"%outputFile%"
) else (
set "prevLine=!currentLine!"
set "currentLine=%%a"
for /f "tokens=1,* delims= " %%b in ("!prevLine!") do set "prevWord=%%b"
for /f "tokens=1,* delims= " %%c in ("!currentLine!") do set "currentWord=%%c"
if "!prevWord!" neq "!currentWord!" echo %%a>>"%outputFile%"
)
)
endlocal
(for /f %%a in ('type "%inputFile%" ^| find /c /v ""') do set /a lines=%%a) >nul
set /a "lastLine=%lines% - 1"
(for /f "usebackq skip=%lastLine% delims=" %%a in ("%inputFile%") do echo %%a)
(for /f "usebackq skip=%lastLine% delims=" %%a in ("%inputFile%") do echo %%a) >>"%outputFile%"
endlocal
试试这个:
@echo off
setlocal enabledelayedexpansion
:: Prompt for input file name and set output file name
set /p "inputFile=Enter file name: "
set "outputFile=!inputFile!_filtered.txt"
:: Initialize variables to manage lines and statuses
set "prevStatus=none"
set "lineCount=0"
set "firstLine=none"
set "secondLine=none"
:: Read each line from the file
for /f "usebackq delims=" %%a in ("%inputFile%") do (
set /a "lineCount+=1"
set "currentLine=%%a"
:: Always store the last line
set "lastLine=%%a"
:: Special handling for the first and second line
if !lineCount! equ 1 (
set "firstLine=%%a"
) else if !lineCount! equ 2 (
set "secondLine=%%a"
) else (
:: Split the line to get the status (OPEN or CLOSE)
for /f "tokens=1,* delims= " %%b in ("!currentLine!") do set "currentStatus=%%b"
:: Compare the current status with the previous one and write to output if different
if not "!prevStatus!" equ "!currentStatus!" (
echo !prevLine!>>"%outputFile%"
)
:: Update previous status and line
set "prevStatus=!currentStatus!"
set "prevLine=%%a"
)
)
:: Output the first two and last line
(
echo !firstLine!
echo !secondLine!
echo !lastLine!
) >> "%outputFile%"
endlocal
这是我所做的更改以及原因:
在原始脚本中,曾尝试专门管理前两行,但并没有完全达到目的,因为只有第一行被区别对待。为了解决这个问题,我将脚本修改为:
firstLine
和 secondLine
)分别捕获并存储第一行和第二行。这可确保这些行不会受到随后的状态比较逻辑的影响,否则可能会导致它们被错误地过滤掉。您的初始脚本尝试实现一种机制来检测状态变化,但由于执行顺序和变量处理而面临问题:
原始脚本尝试包含最后一行,但由于确定文件中的总行数并相应调整处理的复杂性而未能正确处理它。为了简化并确保准确性:
最终输出处理:
处理文件后,脚本现在输出:
这种方法可确保根据您的规范过滤文件内容,同时还简化了一些逻辑并使脚本更健壮且更易于维护。