如何删除文件中的行(或文本中给出行)在批次? [英] How to remove lines (or text in given lines) from file in batch?
问题描述
我的目标是创建批处理脚本,删除线本身(如果可能)或文本从文件中给定的线,如CSS文件。
CSS文件
行
199
200 / * * K2START /
201包含.foo {宽度:1}
202的.bar {宽度:2}
203 / * * K2END /
204
我想删除行 200
和行之间的文本 203
(包括注释部分)。行号可以被改变,因为其他人可能会添加更多的文本文件。所以我的选择是找文本 K2START
和 K2END
然后删除这些行之间的文本。
批处理脚本
这将返回200:/ * * K2START /
这是伟大的。但我只是无法弄清楚如何获得行号值200和使用它。
FINDSTR / NK2START%K2DIR%\\ K2 smartforms设计\\样式\\主题\\ Sun.css
我也通过CSS文件的每一行试图循环实践循环,但不知何故,而不是进入在CSS文件中每行文本,整个路径字符串(即%K2DIR%\\ K2 smartforms设计师\\样式\\主题\\ Sun.css
)被视为令牌。
FOR / F令牌= *%% G IN(%K2DIR%\\ K2 smartforms设计\\样式\\主题\\ Sun.css),DO(
@echo %%摹
)
关闭@echo
SETLOCALREM获取的开始和结束行的无用部分的数
设置开始=
FOR / Fdelims =%%一个在('FINDSTR / NK2START K2ENDsmartforms设计师\\样式\\主题\\ Sun.css')做(
如果没有定义启动(
集开始= %%一
)其他(
设置结束= %%一
)
)REM复制所有行,除在起始端部的那些
(FOR / F令牌= 1 * delims =:一个在('FINDSTR / N^smartforms设计师\\样式\\ \\主题Sun.css')%%做(
如果%%一个LSS%起步%回波(%% b
如果%%一个GTR%结束%回波(%% b
))GT; newFile.css
My goal is to create batch script that removes line itself (if possible) or text in given lines from files such as css file.
css file
line
199
200 /* K2START */
201 .foo { width: 1 }
202 .bar { width: 2 }
203 /* K2END */
204
I want to remove the text between line 200
and line 203
(including comment part). The line number could be changed because other people may add more text into the file. So my option is to find text K2START
and K2END
then remove text between those lines.
batch script
This returns "200: /* K2START */"
which is great. But I just can't figure out how to get the line number value "200" and use it.
findstr /N "K2START" "%K2DIR%\K2 smartforms Designer\Styles\Themes\Sun.css"
I also tried to loop through each line of css file to practice loop but somehow instead of going into each line of text in the css file, the entire path string (i.e. "%K2DIR%\K2 smartforms Designer\Styles\Themes\Sun.css"
) is treated as token.
FOR /F "tokens=*" %%G IN ("%K2DIR%\K2 smartforms Designer\Styles\Themes\Sun.css") DO (
@echo %%G
)
@echo off
setlocal
rem Get start and end line numbers of the unwanted section
set start=
for /F "delims=:" %%a in ('findstr /N "K2START K2END" "smartforms Designer\Styles\Themes\Sun.css"') do (
if not defined start (
set start=%%a
) else (
set end=%%a
)
)
rem Copy all lines, excepting the ones in start-end section
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "smartforms Designer\Styles\Themes\Sun.css"') do (
if %%a lss %start% echo(%%b
if %%a gtr %end% echo(%%b
)) > newFile.css
这篇关于如何删除文件中的行(或文本中给出行)在批次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!