批处理文件 - 更换5号线在一个文本文件 [英] Batch files - Replacing the 5th line in a text file

查看:217
本文介绍了批处理文件 - 更换5号线在一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间在网上寻找一个答案,但我一直无法得到任何工作。

I have spent some time online looking for an answer to this but I have been unable to get anything to work.

我有1700行的文本文件。不要紧,什么是对这些行,但我总是希望用相同的字符串来代替5号线。

I have a text file with around 1700 lines. It doesn't matter what is on any of these lines but I always want to replace line 5 with the same string.

例如文本文件名为test.txt的。该行去是这样的:

For Example the text file is called test.txt. The lines go something like:




  1. C

  2. D

  3. FMOD'在这里随机的东西

  4. ˚F

我一直想更换5号线(它总是FMOD启动,但之后有随机的东西)与一组字符串。这有许多工作要做相当多的时间和手动的时刻完成的,这将真正帮助我,如果可以通过它无论如何使用,其他人可能想用这个,所以我希望做一个批处理文件来实现所有原生支持,而无需使用额外的文件,该批处理文件。

I always want to replace the 5th line (which always starts with FMOD but had random stuff after that) with a set string. This has to be done quite a few times and is done manually at the moment, It would really help me if it could be done through a batch file which is used anyway and other people might want to use this so I was hoping to do it all natively in the batch file without the use of additional files.

大部分AP $ P $提前pciated,

Much appreciated in advance,

推荐答案

使用纯天然批次:

@echo off
setlocal disableDelayedExpansion

set "file=yourFile.txt"
set "newLine5=NewLine5Here"

>"%file%.new" (
  for /f "delims=" %%A in ('findstr /n "^" "%file%"') do for /f "delims=:" %%N in ("%%A") do (
    set "ln=%%A"
    setlocal enabableDelayedExpansion
    if "!ln:~0,6!" equ "5:FMOD" (echo(!newLine5!) else echo(!ln:*:=!
    endlocal
  )
)
move /y "%file%.new" "%file%" >nul

以上仅限于每行〜8191字节。速度可能不会为1700线是坏的,但它是相对较慢。

The above is limited to ~8191 bytes per line. The speed will probably not be bad for 1700 lines, but it is relatively slow.

生命是非常简单的,如果你用我的 JREPL.BAT工具 - 混合的JScript /批处理脚本,可以做一个普通的前pression查找/替换文件的内容,包括合并用户提供的JScript code来定制结果的选项。它是纯粹的脚本,将任何Windows计算机上本地运行,从XP起。

Life is really simple if you use my JREPL.BAT utility - a hybrid JScript/batch script that can do a regular expression find/replace on file contents, including the option to incorporate user supplied JScript code to tailor the result. It is pure script that will run natively on any Windows machine from XP onward.

这JREPL解决方案 比任何纯批次的解决方案更快,更可靠的:

This JREPL solution is much faster and more robust than any pure batch solution:

jrepl "^FMOD.*" "NewLine5Here" /jbegln "skip=(ln!=5)" /f "yourFile.txt" /o -

这篇关于批处理文件 - 更换5号线在一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆