批处理:searchstring以跳过上面的行,并将结果写入新文件

批处理:searchstring以跳过上面的行,并将结果写入新文件,第1张

概述批处理:searchstring以跳过上面的行,并将结果写入新文件

我已经成功地编写了一个脚本,它接受一个string在一个特定的文件中search,然后输出它首次出现的那一行,然后我把这个值放到一个for循环中,然后跳过parsing这个行数并写出它内容到一个新的文件。 但是,我没有得到空白线,我觉得很难解决。

我正在search的string是“/]”,caching它发生的行号,然后用逗号将它累加到一个variables中。 然后我再把这个variables放到一个for循环中,然后检索第一个出现的值作为我最后的“跳过这行数”variables,然后我在底部使用for循环,再次读取该文件,并将其值写入一个新的文件,并跳过文件开头的行数。

所以inheritance了我上面描述的脚本的部分:

setlocal enabledelayedexpansion setlocal enableextensions set live_svn_access_file=c:csvndataconfsvn_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% )

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

在使用bash / linux的特定行之后创build一个空行

setlocal enabledelayedexpansion setlocal enableextensions set live_svn_access_file=c:csvndataconfsvn_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 )

总结

以上是内存溢出为你收集整理的批处理:searchstring以跳过上面的行,并将结果写入新文件全部内容,希望文章能够帮你解决批处理:searchstring以跳过上面的行,并将结果写入新文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1159611.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存