【问题标题】:Batch: Search for string to skip lines above and write results to new file批处理:搜索字符串以跳过上面的行并将结果写入新文件
【发布时间】:2011-10-25 06:27:18
【问题描述】:

我已经成功编写了一个脚本,该脚本接受一个字符串以在特定文件中搜索,然后输出它第一次出现的行,然后我将该值放入 for 循环并跳过解析该行数并将其内容写入新文件。但是我没有得到空白行,我觉得解决起来很困难。

我要搜索的字符串是“/]”,缓存它出现的行号,然后用逗号分隔将它累积到一个变量中。然后我再次将该变量放入for循环并检索第一个出现的值作为我的最终“跳过此行数”-变量,然后我在底部for循环中使用它再次读取该文件并将其值写入一个新文件,并在文件开头跳过该数量的行。

下面是我上面描述的脚本部分:

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%
)

我已经重写了脚本以匹配工作脚本,这是更改后的脚本:

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
)

【问题讨论】:

  • if not defined read-input echo. >> %of% 行生成一个空格而不是空行,您可以更改为(>> %of% echo.)。你应该避免echo.,它很慢而且可能会失败,最好使用echo(。如果%skip% 为零,skip=%skip% 选项将失败
  • 我已经更改了脚本以符合您的建议,请确认我对您的理解正确。
  • 恕我直言,您的代码现在应该可以稳定运行
  • @Niklas 您是否看到从原始文件中删除了感叹号?我有这个变种,但我仍然看到这个问题。
  • @Jared 我没有用感叹号测试,因为它们不会出现在我的文件中。

标签: windows batch-file for-loop blank-line


【解决方案1】:

您有两个主要问题,一个 FOR /F 无法读取空行(正如您发现的那样),如果您使用延迟扩展,您会遇到set read-input=%%i 行中的感叹号和插入符号的问题。

您可以解决这两个问题,即使用 findstr 在每行前加上行号的空行。 第二个是延迟切换技术。

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
)

【讨论】:

  • 谢谢,但我不能在 for 循环中使用命令 (findstr),它必须是一个文件,因为我使用“Skip”选项来跳过一些行。
  • 你也可以在这个结构中使用skip-option
  • 我会试试的。谢谢。顺便说一句, echo(!var! 是否正确关闭?
  • 是的 "echo(" 是最好的方式来回显一个空行而不会出现问题并避免输出 "ECHO is On"
  • @jeb:是的,你是对的!我从来没有考虑过这个案例......但是,我仍然认为( 是不够的,因为它会干扰对带括号的程序的审查。也许我们可以使用标准分隔符,;=:` (I like echo`),你怎么看?我为这一切道歉,但我认为我们就这一点达成一个标准很重要;-)
猜你喜欢
  • 1970-01-01
  • 2021-11-25
  • 2023-03-29
  • 1970-01-01
  • 2014-03-12
  • 2018-01-21
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
相关资源
最近更新 更多