Find until that line and replace with another text

2019-09-01 06:44发布

I am trying to write a windows batch script that it will search until

Example:

example1
example2
example3
example4
    <jar href="example.jar" main="true"/>

So i want to replace first part until ( <jar href ) of the text with another text. I mean the first the first 4 lines will be replaces with another text. Is there any find until and replace method in batch script.

2条回答
叛逆
2楼-- · 2019-09-01 07:17

You can do it but editing XML it isn't very easy using DOS, and in order to make it happen, you are going to first need to start your batch file like this:

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
::
:: script content here
::
PAUSE

Then, you will need to understand how you can escape the '<' and '>' chars with delayed expansion using the '^' character.

I would recommend you also review the website called DosTips .

Finally, I wrote a script that edits property files, which is an example that you might be able to pull some ideas from.

Finally, if what you are editing is pure XML, you might have some success with XML Starlet in dos script.

查看更多
ら.Afraid
3楼-- · 2019-09-01 07:20

Your specific scenario is fairly easy to implement in batch. I'm assuming you want to preserve the entire line that contains <jar href

You don't say where the replacement text is coming from. I'm assuming it comes from another file.

@echo off
set file1="test.txt"
set file2="replacement.txt"
set output="new.txt"

for /f "delims=:" %%N in ('findstr /n /c:"<jar href" %file1%') do (
  set /a skip=%%N-1
  goto :break
)
:break
(
  type %file2%
  more +%skip% %file1%
)>%output%
查看更多
登录 后发表回答