gpt4 book ai didi

windows - 批处理 : Search for string to skip lines above and write results to new file

转载 作者:可可西里 更新时间:2023-11-01 09:26:00 25 4
gpt4 key购买 nike

我已经成功地编写了一个脚本,它接受一个字符串来在特定文件中搜索,然后输出它第一次出现的行,然后我将该值放入 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
)

最佳答案

你有两个主要问题,一个 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
)

关于windows - 批处理 : Search for string to skip lines above and write results to new file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7885437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com