批处理 - 我想在一个文件中有一个变量来搜索和替换该变量的结果在另一个文件 [英] Batch - I would like to search with a variable in one file and replace that variable result in another file

查看:130
本文介绍了批处理 - 我想在一个文件中有一个变量来搜索和替换该变量的结果在另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个变量来搜索一个文件,然后拿这个变量的结果并替换另一个文件中的一些文本的一部分。

有关在的1.txt〔实施例我有一个文本行:
类名= X

根据文本您搜索类名该行的文件在X =改变所以,我将不得不使用变量与类名=

搜索

我会然后搜索中X字类=后的另一个文件中,用ShipDummy代替。我们将调用第二个文件2.txt。第二个文件在X会被发现是这样的:类= X。不过,我想它是类= ShipDummy

我会搜索类似的1.txt布局,但没有姓名的一部分,和的1.txt不会得到任何ShipDummy

我也想提出2.txt备份替换发生之前。

此外,还有很多类名=行中的1.txt,都用不同的x值。我想它给一个变量对每个x值,然后做替换为2.txt该值。例如:
在1.txt的类名= X(更换类= X在类= ShipDummy 2.txt)

类名= Y 1.txt文件中(替换用CLASS = ShipDummy 2.txt类= Y)

类名= Z 1.txt文件中(替换用CLASS = ShipDummy 2.txt CLASS = Z)

等等....

请记住,在上面我使用的x,y和z这里变量。

我会怎么做这一切的?我是新来的编码和其相当复杂。我想这个批处理文件。

我已经是这样的,但我失去了:

 关闭@echoSETLOCAL ENABLEDELAYEDEXPANSIONFOR / F令牌= *%%一中(1.txt的)做SETLOCAL ENABLEDELAYEDEXPANSIONFOR / Fdelims =%%的(!VAR2)一个做呼应VAR1:名称= %%阿!出口


解决方案

大概要解决这个最简单的方法是从相反的方向接近它。

阅读在2.txt每一行。如果该行的templpate 类= X 匹配,则解析出X和搜索,看看是否类名= X 存在1.TXT。如果它确实存在,然后写更换线路,否则写原线。

您可以搜索使用FINDSTR在文件中的字符串。你不关心输出,所以输出重定向到空。在&放大器;&安培; 运营商有条件地执行code。如果先前命令(FINDSTR)是成功的(匹配)

 关闭@echo
SETLOCAL enableDelayedExpansion
> 2.txt.new(
  FOR / Fdelims =%%的(2.txt)在做(
    设置LN = %% A
    如果LN:〜!0,6! ==类=FINDSTR / C:类名= LN:〜!6! 1.TXT>空&放大器;&安培;设置LN = CLASS = ShipDummy
    回音!LN!
  )

此举2.txt.new 2.txt

上面的脚本是未经测试,但这个概念应该工作。有很多改进的余地。

I would like to search using a variable in one file, then take that variable result and replace part of some text in another file.

For exmaple in "1.txt" I have a text line of: ClassName=X

X changes within the file depending on the line of text that you search ClassName= So I would have to search using a variable with ClassName=

I would then search for X in another file after the word "Class=", replacing it with the word "ShipDummy". We'll call the second file "2.txt". X within the second file would be found like this: Class=X. However, I would like it to be Class=ShipDummy

I would search similar to the layout in 1.txt, but without the "Name" part, and 1.txt wont get ShipDummy either.

I would also like to make a backup of 2.txt before the replacement takes place.

Also, there are many ClassName= lines in 1.txt, all with different x values. I'd like it to give a variable for each x value, then doing the replace for that value in 2.txt. For example: ClassName=x in 1.txt (replace Class=x in 2.txt with Class=ShipDummy)

ClassName=y in 1.txt (replace Class=y in 2.txt with Class=ShipDummy)

ClassName=z in 1.txt (replace Class=z in 2.txt with Class=ShipDummy)

and so on....

Keep in mind, in the above I am using x, y and z as variables here.

How would I do all this? I am new to coding and its quite complex. I'd like this in a batch file.

What I have is something like this, but I'm lost:

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=*" %%a in (1.txt) do 

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "delims=" %%A in ("!var2!") do echo !var1:Name=%%A!

exit

解决方案

Probably the easiest way to solve this is to approach it from the reverse direction.

Read each line in in 2.txt. If the line matches the templpate Class=X, then parse out X and search to see if ClassName=X exists in 1.txt. If it does exist, then write the replacement line, else write the original line.

You can search for a string within a file using FINDSTR. You don't care about the output, so output is redirected to null. The && operator conditionally executes code if the prior command (FINDSTR) was successful (matched).

@echo off
setlocal enableDelayedExpansion
>2.txt.new (
  for /f "delims=" %%A in (2.txt) do (
    set "ln=%%A"
    if "!ln:~0,6!" == "Class=" findstr /c:"ClassName=!ln:~6!" 1.txt >null && set "ln=Class=ShipDummy"
    echo !ln!
  )
)
move 2.txt.new 2.txt

The script above is untested, but the concept should work. There is lots of room for improvement.

这篇关于批处理 - 我想在一个文件中有一个变量来搜索和替换该变量的结果在另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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