0

I have a file where I search for a specific text and want to replace it with another. For example:

<role-name>test</role-name>

this is for example what I want to replace it with:

<role-name>file</role-name>

The problem here would be that in the <role-name> tag might be other text then "test".

How can I find the whole line and replace it with the text that I need?

Or maybe I can retrieve the line number of the tag and replace it whole, but again I have no idea how to do that :-)

Sebastian
  • 617
  • 2
  • 10
  • 30
  • check [here](http://stackoverflow.com/a/32757504/388389) – npocmaka Apr 07 '16 at 09:38
  • @npocmaka thx but I can't use external files and also my main problem here is that the string between the tags can differ. So what I want to do is to search for and where I have found it, I must replace the whole line with another string. – Sebastian Apr 07 '16 at 14:01
  • @Mofi it is not a duplicate because in that question he knew exactly the string that had to be replaced. In my case I had to replace the entire line based on a partial string. – Sebastian Apr 10 '16 at 10:27

2 Answers2

0

The key to doing this without external code is using the string substitution expression. I assume you want to parameterize the tag and value strings. This would also depend on the sequence not having any newline characters.

C:>type seb.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET EXITCODE=0

SET "TAG=%~1"
SET "FROMVAL=%~2"
SET "TOVAL=%~3"

FOR /F "usebackq tokens=*" %%a IN (`TYPE "seb.html"`) DO (

    echo %%a

    SET S1=%%a
    SET S2=!S1:^<%TAG%^>%FROMVAL%^</%TAG%^>=^<%TAG%^>%TOVAL%^</%TAG%^>!
    ECHO !S2!
)

EXIT /B %EXITCODE%

C:>type seb.html
<role-name>test</role-name>

C:>seb role-name test file
<role-name>test</role-name>
<role-name>file</role-name>
<role-name></role-name>
<role-name></role-name>

C:>seb role-name test ""
<role-name>test</role-name>
<role-name></role-name>
<role-name></role-name>
<role-name></role-name>

C:>seb role-name "" "file"
<role-name>test</role-name>
<role-name>test</role-name>
<role-name></role-name>
<role-name>file</role-name>
lit
  • 14,456
  • 10
  • 65
  • 119
  • thank you for your answer but it does not work in my case due to the FromVal variable. It can contain "test" or "test1" or anything else. I must replace the whole line with the new string somethingelse. This bat file I want to use it as an automation to build a package with maven and I do not want to edit it every time I build the package. – Sebastian Apr 07 '16 at 14:49
  • Ok, so the tag and value to be replaced now are taken from the command line. There really should be some validation that the parameters are given. That is left as an exercise to the reader. Will this work for you? – lit Apr 07 '16 at 14:54
  • I am afraid that I did not understood what you meant. The thing is that I know what value to put in that tag, but I do not know what value is before I replace it. For example when I build the application for the first time the tag must be empty () but at the second build I could have in that tag "test" or "int" and I want to replace it with "prod". Thank you :-) – Sebastian Apr 07 '16 at 15:05
  • I changed it so that an empty string can be specified for the values. The first time you run it, use `""` as the current value parameter. – lit Apr 07 '16 at 16:30
  • Thank you but this will still not work. I wouldn't know what value is between the tags so I can't call the script with specific values and I still have to consider that maybe someone will use this script and would not know when it should call it with "" or with a value – Sebastian Apr 08 '16 at 06:10
0

The problem was that I was reading from a file and the string that I wanted to compare came in a for loop. This is my code that reads from file and writes the code that I needed, if anyone needs it :

set "nameofapp=%<display-name>"
set "nameofapp_country=%    <display-name>ASD</display-name>"
set "rolename=%<role-name>"
set "rolename_country=%                <role-name>ASD-role</role-name>"
set "propertiesfile=%<env-entry-value>"
set "properties_file=%        <env-entry-value>java-asd.properties</env-entry-value>"
set "web=src\main\webapp\WEB-INF\web.xml"
set "web_1=web.xml"

(for /f "delims=" %%i in (%web%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion

    set "name=!line:%nameofapp%=%!"
    if not !name! == !line! (
        set "line=%nameofapp_country%"
    )

    set "role=!line:%rolename%=%!"
    if not !role! == !line! (
        set "line=%rolename_country%"
    )

    set "property=!line:%propertiesfile%=%!"
    if not !property! == !line! (
        set "line=%properties_file%"
    )
    echo(!line!
    endlocal
))>"%web_1%"
del %web%
Move %web_1%  "src\main\webapp\WEB-INF\"
Sebastian
  • 617
  • 2
  • 10
  • 30