Batch: Search for string to skip lines above and w

2019-02-19 02:39发布

问题:

I've successfully written a script which takes a string to search for in a specific file, and then outputs the line where it first occurs, and then I take that value into a for-loop and skips parsing that number of lines and write its contents to a new file. However I do not get blank lines which I find quite problematic to solve.

The string I'm searching for is "/]", caches the line number where it occurs, then accumulates it into a variable with comma-seperation. I then take that variable into a for-loop again and retrieve the first occurring value as my final "skip this number of lines"-variable, which I then use at the bottom for-loop which reads that file again and writes its values to a new files and skips that number of lines at the beginning of the file.

So heres the part of the script that does what I'm describing above:

setlocal enabledelayedexpansion
setlocal enableextensions
set live_svn_access_file=c:\csvn\data\conf\svn_access_file
set of=c:\test.txt

for /f "Tokens=1 Delims=:" %%i in ('findstr /n /c:"/]" %live_svn_access_file%') do (
    rem cache value in a variable
    set line=%%i
    rem accumulate data to one variable
    if defined line set skip=!skip!, !line!
)
rem strip first two characters in variable ", "
set skip=!skip:~2!
rem strip everything except first value in array
for /f "Tokens=1 Delims=," %%i in ('echo !skip!') do (
    rem store value in a variable
    set skip=%%i
)
rem calculate lines - 1 (arithmetic)
set /a skip=!skip!-1
set skip=!skip!

if not defined skip set error=Could not automatically find which parts to skip from live svn_access_file && goto error-handler

for /f "Tokens=* Delims= Skip=%skip%" %%i in (%live_svn_access_file%) do (
    rem cache value in a variable
    set read-input=%%i
    rem write and append content of variable to output-file
    echo !read-input! >> %of%
)

I've rewritten the script to match the working one, this is the changed script:

setlocal enabledelayedexpansion
setlocal enableextensions
set live_svn_access_file=c:\csvn\data\conf\svn_access_file
set of=c:\test.txt

for /f "Tokens=1 Delims=:" %%i in ('findstr /n /r /c:"\[..*\]" %live_svn_access_file%') do (
    rem cache value in a variable
    set line=%%i
    rem accumulate data to one variable
    if defined line set skip=!skip!, !line!
)
rem take the 2nd sections linenumber into a variable, skipping the first [*foo*]
for /f "Tokens=2 Delims=," %%i in ('"echo !skip!"') do (
    rem store value in a variable
    set skip=%%i
)
rem add 1 line to skip from retrieved value
set /a skip=!skip!-1

rem verify that number of lines to skip has been successfully retrieved, if not go to error-handler
if not defined skip set error=Could not automatically find which parts to skip from live svn_access_file && goto error-handler
if ["%skip%"] LSS ["1"] set error=Number of lines to skip was less than 1, this will most likely fail && goto error-handler

rem read live svn_access_file, but skip X-lines and write to output-file
setlocal DisableDelayedExpansion
for /f "usebackq Delims= Skip=%skip%" %%i in (`"findstr /n ^^ %live_svn_access_file%"`) do (
    rem cache value in a variable
    set read-input=%%i
    rem write and append content of variable to output-file
    setlocal EnableDelayedExpansion
    rem strip line number prefix
    set read-input=!read-input:*:=!
    rem write read lines to output-file
    echo(!read-input!>>%of%
    endlocal
)

回答1:

You got two main problems, a FOR /F can't read empty lines (as you discover) and if you use delayed expansion you got trouble with exclamation marks and carets in the line set read-input=%%i.

You can solve both, the empty lines with prefixing each line with a line number by using findstr. The second with the delayed toggling technic.

SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!" This removes the prefix
    echo(!var!
    ENDLOCAL
)