batch-file - 使用批处理文件删除特定的行号
问题描述
我正在尝试制作一个脚本,.txt
通过它的编号删除文件的特定行。我已经明白了:
@echo off
for /f "skip=1delims=*" %%a in (test.txt) do (
echo %%a >>newfile.txt
) >nul
它工作正常并删除行号“1”。但是如果我把更多的行删除,它就不起作用,见下文:
@echo off
for /f "skip=1,5,8) delims=*" %%a in (test.txt) do (
echo %%a >>newfile.txt
) >nul
什么是错?
解决方案
正如评论中已经指出的那样,skip
没有帮助。find /v /n ""
如果行号 (token1) 不在列表中,则添加行号 (with ) 并输出不带行号的原始行 (token2):
@echo off
(for /f "tokens=1,* delims=[]" %%a in ('type test.txt^|find /v /n ""') do (
echo/%%a|findstr /x "1 5 8" >nul || echo/%%b
))>newfile.txt
(findstr
开关x
对于比较整数很重要,所以2018
没有找到findstr "1"
)
||
就像“如果上一个命令 ( findstr
) 失败,那么”
推荐阅读
- python - 在 pandas 中在哪里添加多索引级别?
- python - Python:反转列表中的每 2 个项目
- javascript - (plugin babel) Error: Plugin/Preset files are not allowed to export objects, only functions
- ruby-on-rails - 我可以忽略 `node_modules`、`tmp` 和 `storage` 文件夹吗?
- python - 是否可以通过循环将这些有问题的元素更改为零以保持可持续性?
- python - 使用 Python OpenCV 检测图像中的所有圆圈(光学标记识别)
- docker - Docker 容器未注册 ips
- reactjs - 如何从 localStorage 中获取 key 的值并通过 hooks 将其输入?
- asp.net-core - 设置 ASP.NET 身份多因素
- docker - 我应该如何为开发数据库构建 docker 映像?