How to ignore all lines after a specific word on processing lines of a text file in a FOR loop?

狂风中的少年 提交于 2019-12-11 09:07:06

问题


My problem is solved about the columns not about the lines using skip=3. This ignores the very 3 first lines. So what about ignoring all the lines after the first end word in the example below.

1121.19, 1638.66, 16.6098, -4.7339, -4.7339, 2.5, hash:0, -1, -1, -1, -1, 1888, 0, 0
971.597, 1815.17, 21.085, 1.39876E-006, 8, 4, hash:0, -1, -1, -1, -1, 1905, 0, 0
971.597, 1825.45, 21.085, 1.39876E-006, 8, 4, hash:0, -1, -1, -1, -1, 1905, 0, 0
end
tcyc
end
mlop
end
lodm
end
slow
end
blok
end

So I want to ignore all the lines which aren't in my base format which is:

971.597, 1825.45, 21.085, 1.39876E-006, 8, 4, hash:0, -1, -1, -1, -1, 1905, 0, 0

Which code is needed to ignore all the lines from the first end word to the end of the file which would ignore the following lines from example input:

end
tcyc
end
mlop
end
lodm
end
slow
end
blok
end

Here's the code I have so far:

if exist 12.txt del 12.txt
FOR /F "skip=3 tokens=1-8* delims=," %%A IN (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo model:%%H tx:%%A ty:%%C tz:%%B rx:%%D ry:%%F rz:%%E

回答1:


This first batch file checks if all loop variables used in echo line has a non empty string which results in ignoring all lines except the first 3 lines.

@echo off
setlocal EnableExtensions
set "OutputFile=12.txt"
del "%OutputFile%" 2>nul
for /F "usebackq tokens=1-8* delims=," %%A in ("C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt") do (
    if "%%A" NEQ "" (
        if "%%B" NEQ "" (
            if "%%C" NEQ "" (
                if "%%D" NEQ "" (
                    if "%%E" NEQ "" (
                        if "%%F" NEQ "" (
                            if "%%H" NEQ "" (
                                >>"%OutputFile%" echo model:%%H tx:%%A ty:%%C tz:%%B rx:%%D ry:%%F rz:%%E
                            )
                        )
                    )
                )
            )
        )
    )
)
endlocal

This second batch file processes the lines up to first line containing the string end case-insensitive.

@echo off
setlocal EnableExtensions
set "OutputFile=12.txt"
del "%OutputFile%" 2>nul
for /F "usebackq tokens=1-8* delims=," %%A in ("C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt") do (
    if /I "%%A" EQU "end" goto ContinueAfterLoop
    >>"%OutputFile%" echo model:%%H tx:%%A ty:%%C tz:%%B rx:%%D ry:%%F rz:%%E
)
:ContinueAfterLoop
endlocal

By the way: %USERPROFILE% is same as C:\Users\Sherlock if user account name is Sherlock.



来源:https://stackoverflow.com/questions/32367624/how-to-ignore-all-lines-after-a-specific-word-on-processing-lines-of-a-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!