批处理:替换文本文件中的一行 [英] batch: replace a line in a text file

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

问题描述


我正在尝试替换此行:


I'm trying to replace this line:

#        forward-socks5   /               127.0.0.1:9050 .

与此:

        forward-socks5   /               127.0.0.1:9050 .

此行属于一个配置文件,必须通过从头开始删除#号来启用(未注释),除了将其替换为没有#号的其他行,我没有想到更好的方法. br/> 任何其他想法或方式都将非常有用.

this line belongs to a config file that has to be enabled (UN-commented) by deleting the # sign from the beginning and I could not thought of a better way other than replacing the line with another without the # sign.
any other thoughts or ways would be very useful.

顺便说一句,文本之前的空格也在那里.
我已经粘贴了原始文件中的文本.

btw, the spaces before the text are there also.
I have pasted the text as it was in the original file.

预先感谢


我已经设法通过使用找到的两个代码来完成行的添加和删除. 我唯一的问题是,以下代码消除了输出文件中的所有惊叹号!


I have somehow managed to do the line addition and removing using two peaces of code that I've found. my only problem is that the following code removes every bit of exclamation in the output file!

@echo off

:Variables
SETLOCAL ENABLEDELAYEDEXPANSION
set InputFile=config.txt
set OutputFile=config-new.txt
set _strFind=#        forward-socks5   /               127.0.0.1:9050 .
set _strInsert=        forward-socks5   /               127.0.0.1:9050 .
set i=0

:Replace
for /f "usebackq tokens=1 delims=[]" %%A in (`find /n "%_strFind%" "%InputFile%"`) do (set _strNum=%%A)
for /f "usebackq delims=" %%A in ("%InputFile%") do (
  set /a i = !i! + 1
  echo %%A>>"%OutputFile%"
  if [!i!] == [%_strNum%] (echo %_strInsert%>>"%OutputFile%")
)
type %OutputFile% | findstr /i /v /c:"%_strFind%">config-new2.txt

我想知道是否有任何一种方法可以一步完成查找/删除/添加行(不是我的两步)...

I was wondering if there is any way to do both the find/delete/add line in one step (not two steps as mine)...

推荐答案

包含!的行已损坏,因为在FOR变量(%%A)扩展之后发生了延迟扩展.这可以通过禁用延迟扩展来解决.在循环中,将%%A的值保存在变量中,然后打开延迟扩展,处理该行,然后再将其关闭.

Lines containing ! are corrupted because delayed expansion occurs after FOR variable (%%A) expansion. That could be solved by starting out with delayed expansion disabled. Within the loop you save the value of %%A in a variable, and then toggle delayed expansion on, process the line, and then toggle it back off.

您无需在SET/A计算中扩展变量.

You do not need to expand the variable within a SET /A computation.

通过在循环中添加IF语句,您可以一步一步完成所有操作.

You can do everything in one step by adding an IF statement within your loop.

实际上,您甚至根本不需要SET/A或FINDSTR.您的IF语句可以测试该行是否与您的搜索字符串匹配.您实际上并不需要因问题而延迟扩展.

In fact, you don't even need SET /A or FINDSTR at all. Your IF statement can test if the line matches your search string. You don't really need delayed expansion for your problem.

将整个循环封装在parens中,然后仅重定向到输出文件一次,效率更高.

It is more efficient to enclose the entire loop within parens and redirect to your output file just once.

@echo off
setlocal disableDelayedExpansion

:Variables
set InputFile=config.txt
set OutputFile=config-new.txt
set "_strFind=#        forward-socks5   /               127.0.0.1:9050 ."
set "_strInsert=        forward-socks5   /               127.0.0.1:9050 ."

:Replace
>"%OutputFile%" (
  for /f "usebackq delims=" %%A in ("%InputFile%") do (
    if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
  )
)

这篇关于批处理:替换文本文件中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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