如何删除文本文件中的特定行(Windows)

问题描述:

使用任何Windows命令,如何完成此操作?如何删除文本文件中的特定行(Windows)

例如删除file.txt中的第2行

嗨我认为不能在命令行中完成,这是一种复杂的要求。

你想更详细地解释你的任务吗?

这里是C#中的代码片段,可以帮助你。

 string filePath = @"c:\temp\test.txt"; 
     string line; 
     string NewText = string.Empty; 

     if (File.Exists(filePath)) 
     { 
      StreamReader file = null; 

      int linecounter = 0; 

      file = new StreamReader(filePath); 
      while ((line = file.ReadLine()) != null) 
      { 
       if(linecounter==1) 
       { 
        linecounter ++; 
        continue; 
       } 


       linecounter ++; 
      } 
      //then save the new text in a file or overwrite your current file 

是的,它可以在BAT脚本中完成。

阅读HELP FORHELP SET,然后尝试这个

@echo off 
setlocal enabledelayedexpansion 
set /a count=0 
for /f "tokens=*" %%a in (t.txt) do (
set /a count=count+1 
if /i !count! NEQ 2 echo %%a 
)