在第17行

时间:2015-05-05 22:15:06

标签: batch-file

我想在.txt文件中创建一个在第17行编写/替换文本的程序。 我试过这个,但它只用于两行

@Echo off
Echo hello >>>>>>>>>>>>>>>>> test.txt

其结果是:

">> was unexpected at this time."

但是这项工作很好:

@Echo off
Echo hello >> test.txt

2 个答案:

答案 0 :(得分:0)

@echo off

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test.txt') do (
   if %%a neq 17 (
      echo(%%b
   ) else (
      echo The new line 17
   )
)) > output.txt
move /Y output.txt test.txt

此解决方案可消除可能出现在行首的冒号。如果需要,可以修复此问题。

答案 1 :(得分:-2)

Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

For x = 1 to 16
    OutP.WriteLine Inp.Readline
Loop
Inp.Readline
OutP.WriteLine wscript.Arguments(0)
Do Until Inp.AtEndOfStream
    OutP.WriteLine Inp.Readline
Loop

将其命名为something.vbs

使用

cscript //nologo something.vbs "Line 17 text" < infile.txt > outfile.txt
相关问题