Search replace .xml file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
smbs
Posts: 4
Joined: 02 Aug 2017 01:40

Search replace .xml file

#1 Post by smbs » 02 Aug 2017 04:56

I want do a s/r on a large .xml file but only want to change the first occurrence of the match using jrepl -Is this possible?
Is there away of limiting number of matches to process?

Thanx

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Search replace .xml file

#2 Post by penpen » 02 Aug 2017 11:45

It is probably possible to search/replace on a xml file using jrepl,
but you should think about using XSLT (Extensible Stylesheet Language Transformations).
For this you might use my "xslt.bat":
http://www.dostips.com/forum/viewtopic.php?p=32941#p32941

This might also help you:
https://www.w3schools.com/xml/xsl_intro.asp

penpen


smbs
Posts: 4
Joined: 02 Aug 2017 01:40

Re: Search replace .xml file

#4 Post by smbs » 03 Aug 2017 03:48

Maybe my mistake--I specifically want to use jrepl.bat as I am trying to learn how to use the many options available.
I should have posted in jrepl.bat thread -how can I move it to other thread--I don't want to double post!
Thanx

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Search replace .xml file

#5 Post by dbenham » 05 Aug 2017 10:49

As penpen and npocmaka have implied, you should use a tool that understands XML if you want to safely modify an XML document. Two XML documents can be formatted very differently, yet be logically identical. An XML unaware tool like JREPL would have a difficult time making the change, whereas something like XSLST would have no problem.

Unless you are in control of the XML input, you may not be able to rely on the format of the document.

But... assuming you can rely on the format, it is possible to use JREPL.BAT

There is no simple option to replace only the first occurrence. But it is easily done with a bit of user supplied JScript and the /JQ option.

If you can guarantee there is at most one match per line, and you are not using the /M option, then:

Code: Select all

jrepl "YourSearch" "set skip=true; $txt='YourReplacement';" /jq /f "input.xml" /o -

If a single line may have multiple matches, or if you are using the /M option (not shown):

Code: Select all

jrepl "YourSearch" "if (skip) $txt=$0; else {set skip=true; $txt='YourReplacement';}" /jq /f "input.xml" /o -

In either case, 'YourReplacement' can be any arbitrary JScript expression that evaluates to your replacement value.


Dave Benham

smbs
Posts: 4
Joined: 02 Aug 2017 01:40

Re: Search replace .xml file

#6 Post by smbs » 05 Aug 2017 13:20

Many thanx as luck would have it --I need to use the /m option so will try and work it out myself.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Search replace .xml file

#7 Post by dbenham » 05 Aug 2017 15:10

smbs wrote:I need to use the /m option so will try and work it out myself.
:?: :?: :?:
My 2nd code works both with and without the /M option. By "(not shown)", I simply meant I did not include the /M option in the example. But it will still work if you add the /M option.


Dave Benham

smbs
Posts: 4
Joined: 02 Aug 2017 01:40

Re: Search replace .xml file

#8 Post by smbs » 08 Aug 2017 15:43

Works great many thanx

Post Reply