使用批处理命令编辑文本文件中的特定行 [英] edit a specific line in text file using batch command

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

问题描述

我有一个包含以下文本的文本文件.

I have a textfile which contains the following text.

模块"

 {
    "ModuleSignature" = "8:MergeModule.6F1248514B3047E99E4EE8A129CB8605"
    "Version" = "8:1.0.0.0"
    "Title" = "8:uoipmsm"
    "Subject" = "8:"
    "Author" = "8:Microsoft"
    "Keywords" = "8:"
    "Comments" = "8:"
    "SearchPath" = "8:"
    "UseSystemSearchPath" = "11:TRUE"
    "TargetPlatform" = "3:1"
    "PreBuildEvent" = "8:"
    "PostBuildEvent" = "8:"
    "RunPostBuildEvent" = "3:0"
    }

在上面;我想更改从工具触发构建时将给出的版本号.

In the above; I want to change the Version number which I will give when I trigger a build from a tool.

我想在批处理文件中传递参数 $ Version ,它必须从我使用的工具中获取版本号,然后在该文本文件中对其进行更新.

I wanna pass a parameter $Version in batch file, it has to take the version number from the tool I use and update the same in that text file.

例如:在上面的文本中,我想将其编码为"Version" ="8:$ Version",因此,当我在触发构建时提供版本号时,它必须在此文本文件中更新该版本号.

For ex: in the above text i wanna code it as "Version" = "8:$Version" hence when ever I provide a version number while triggering a build, it has to update the same in this text file.

请您指导我如何编辑特定行.我是Windows批处理脚本的新手.

Could you please guide me how to edit the specific line. I am new to windows batch scripting.

我要在文本文件中再增加一个点...我必须在行号399中修改版本.因此,批处理文件必须跳至该文本文件中的行号399,然后对其进行修改.请帮助我修复相同的问题...

and i hav to add one more point... in the text file i have to modify the version in the line number 399. So the batch file has to jump to line num 399 in that text file and modify the same. Kindly help me to fix the same ...

我已将以上脚本保存在文本栏中,并与ver.bat相同;并在同一个文件夹中保存了Intext文件.当我提到要替换的行号时,它是从行1到399中删除在"="符号后出现的内容.

I had saved the above script in a text pad and saved the same as ver.bat; and also in the same folder I saved the Intext file. When I mention the line number which to be replaced, it is removing the contents which are present after "=" symbol, from line 1 to 399 .

运行批处理文件之前:

"ModuleSignature" = "8:MergeModule.6F1248514B3047E99E4EE8A129CB8605"
"Version" = "8:1.0.0.0"
"Title" = "8:uoipmsm"
"Subject" = "8:"
"Author" = "8:Microsoft"
"Keywords" = "8:"
"Comments" = "8:"
"SearchPath" = "8:"
"UseSystemSearchPath" = "11:TRUE"
"TargetPlatform" = "3:1"
"PreBuildEvent" = "8:"
"PostBuildEvent" = "8:"
"RunPostBuildEvent" = "3:0"

如果运行批处理文件,我提到的行号是10,tat之后是行号;我得到以下输出;

I mentioned the line number as 10 and after tat if I run the batch file; i get the following output;

"ModuleSignature" = 
"Version" = ""
"Title" = 
"Subject" = 
"Author" = 
"Keywords" = 
"Comments" = 
"SearchPath" = 
"UseSystemSearchPath" = 
"TargetPlatform" = 
"PreBuildEvent" = 
"PostBuildEvent" = 
"RunPostBuildEvent" = 

有什么想法吗?

推荐答案

这应该可以做到.当然,如果您的输入文件发生更改,则必须更改.如果输入文件中有空行(它们不计算在内),则可能还需要进行调整.请注意,您只需要传递版本(而不是整个新行399).在您的示例中,该值为"8:1.0.0.0".最好在字符串中加引号,以防其中有空格. 作为解释,这将:

This should do it. Of course this will have to change if your input file ever changes. You may also have to make an adjustment if there are blank lines in your input file (they don't count). Note that you only need to pass the version (not the entire new line 399). In your example that would be "8:1.0.0.0". Best to quote the string in case there are ever spaces in there. By way of explanation, this will:

  1. 此bat文件必须带有一个参数
  2. 使用FOR LOOP将前398行回显到临时文件
  3. 使用作为参数传递的版本添加新行399
  4. 使用MORE将剩余的行添加到临时文件中
  5. 将临时文件复制到原始文件
  6. 删除临时文件

x

@echo off
REM %1=Version (use quotes if there are spaces in version)

set ReplaceLine=399
set InFile=Test.txt
set TempFile=TempTest.txt
if exist "%TempFile%" del "%TempFile%"

if "%~1"=="" (
   color CF
   echo.This program must be called with an argument!
   pause
   goto :eof
   )

setlocal enabledelayedexpansion
set /A Cnt=1
for /F "tokens" %%a in (%InFile%) do (
   echo.%%a>> "%TempFile%"
   set /A Cnt+=1
   if !Cnt! GEQ %ReplaceLine% GOTO :ExitLoop
   )
:ExitLoop
endlocal

echo."Version" = "%~1">> "%TempFile%"
more +%ReplaceLine% < "%InFile%">> "%TempFile%"
copy /y "%TempFile%" "%InFile%"
del "%TempFile%"
goto :eof

这篇关于使用批处理命令编辑文本文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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