I’m afraid I can’t imagine how to write a correct batch script that will solve the following task.
Could you help ?
I have a text file “2024-04-29_22-23log.txt” where each line (except the first) starts with OPEN or CLOSE followed by a time stamp.
To highlight status changes, I want to keep only the lines where this word is different from the previous line.
But I want to keep the very first twoo lines and also the last line.
Example:
2024-04-29_22-23
CLOSE 22:23
CLOSE 22:24
OPEN 22:25
OPEN 22:26
OPEN 22:27
CLOSE 22:28
CLOSE 22:29
should get:
2024-04-29_22-23
CLOSE 22:23
OPEN 22:25
CLOSE 22:28
CLOSE 22:29
Thanks for your help.
Following works only with alphabetical file name and forgets last line !
@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
smbd91 smbd91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.