首页 > 解决方案 > 使用批处理文件删除特定的行号

问题描述

我正在尝试制作一个脚本,.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

什么是错?

标签: batch-file

解决方案


正如评论中已经指出的那样,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) 失败,那么”


推荐阅读