批次:搜索字符串跳过上面的线条和结果写入新文件 [英] Batch: Search for string to skip lines above and write results to new file

查看:222
本文介绍了批次:搜索字符串跳过上面的线条和结果写入新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地写了一个脚本,它需要一个字符串在一个特定的文件搜索,然后输出在第一次出现的行,然后我把这个值放到一个for循环,并跳过解析该行数和写入其内容的新文件。但是我不明白空行,我觉得比较困难的解决。

我在寻找的字符串是/],它出现缓存的行号,那么它积累与逗号分离变量。然后我去该变量为一个for循环再次检索第一次出现的值作为我的最后跳过这一行数-variable,然后我在底部for循环它读取一遍文件,并将它的值使用一个新的文件,并跳过行数在文件的开头。

脚本因此,继承人的部分,做什么我上面描述:

  SETLOCAL enabledelayedexpansion
SETLOCAL ENABLEEXTENSIONS
设置live_svn_access_file = C:\\ csvn \\ DATA \\的conf \\ svn_access_file
设置= C的:\\ test.txt的FOR / F令牌= 1 Delims =:我%%在('FINDSTR / N / C:/]%live_svn_access_file%')做(
    在一个变量物权缓存值
    集线= %%我
    REM积累数据,以一个变量
    如果定义了一套行跳过=!跳!!行!

REM条第一变量两个字符,
集跳过=跳过:〜!2!
REM剥夺一切,除了在第一阵列值
在/ F令牌= 1 Delims =%%我('回声!跳!')做(
    在一个变量物权存储值
    跳过设定= %%我

REM计算线 - 1(算术)
集/跳过=!跳!-1
跳过设定=!跳!如果没有定义跳跃设置错误=无法自动找到活svn_access_file&放跳过这部分;&安培;转到错误处理FOR / F令牌= * = Delims跳过=%跳跃%%% i的(%live_svn_access_file%)做(
    在一个变量物权缓存值
    集读取输入= %%我
    REM写入和追加变量的内容输出文件
    回音!读取输入! >> %的%

我已经重写剧本以匹配工作之一,这是改变的脚本:

  SETLOCAL enabledelayedexpansion
SETLOCAL ENABLEEXTENSIONS
设置live_svn_access_file = C:\\ csvn \\ DATA \\的conf \\ svn_access_file
设置= C的:\\ test.txt的FOR / F令牌= 1 Delims =:我%%在('FINDSTR / N / R / C:\\ [.. * \\]%live_svn_access_file%')做(
    在一个变量物权缓存值
    集线= %%我
    REM积累数据,以一个变量
    如果定义了一套行跳过=!跳!!行!

REM采取第二部分行号到一个变量,跳过第[*富*]
在/ F令牌= 2 Delims =%%我('回声!跳!')做(
    在一个变量物权存储值
    跳过设定= %%我

REM加1线从获取的值跳过
集/跳过=!跳!-1REM验证号线跳过已成功地检索,如果不能去错误处理的
如果没有定义跳跃设置错误=无法自动找到活svn_access_file&放跳过这部分;&安培;转到错误处理
如果[%跳跃%] LSS [1]设置错误=行数跳过小于1,这极有可能失败和放大器;&安培;转到错误处理REM读活svn_access_file,但跳过的X线和写入到输出文件
SETLOCAL DisableDelayedExpansion
在/ F有usebackq Delims = =跳过跳过%%%%我(`FINDSTR / N ^^%live_svn_access_file%')做(
    在一个变量物权缓存值
    集读取输入= %%我
    REM写入和追加变量的内容输出文件
    SETLOCAL EnableDelayedExpansion
    REM带状线数preFIX
    集读取输入=读输入:*:=!
    REM写读线到输出文件
    回声(读取输入>!>将%%
    ENDLOCAL


解决方案

您有两个主要问题,一个FOR / F无法读取空行(如你发现),如果你使用延迟扩展,你得到了与惊叹号麻烦并在该行插入符设置读取输入= %%我

您可以解决这两个,空线,$ P $使用FINDSTR pfixing行号的每一行。
与延迟肘工艺的第二位。

  SETLOCAL DisableDelayedExpansion
FOR / F有usebackq delims =%%一中(`FINDSTR / N ^^ t.txt')做(
    设置VAR = %%一个
    SETLOCAL EnableDelayedExpansion
    设置VAR = VAR:*:!=这将删除preFIX
    回声(!VAR!
    ENDLOCAL

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
)

解决方案

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
)

这篇关于批次:搜索字符串跳过上面的线条和结果写入新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆