Page 1 of 1
Search replace .xml file
Posted: 02 Aug 2017 04:56
by smbs
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
Re: Search replace .xml file
Posted: 02 Aug 2017 11:45
by penpen
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#p32941This might also help you:
https://www.w3schools.com/xml/xsl_intro.asppenpen
Re: Search replace .xml file
Posted: 02 Aug 2017 15:49
by npocmaka_
Re: Search replace .xml file
Posted: 03 Aug 2017 03:48
by smbs
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
Re: Search replace .xml file
Posted: 05 Aug 2017 10:49
by dbenham
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
Re: Search replace .xml file
Posted: 05 Aug 2017 13:20
by smbs
Many thanx as luck would have it --I need to use the /m option so will try and work it out myself.
Re: Search replace .xml file
Posted: 05 Aug 2017 15:10
by dbenham
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
Re: Search replace .xml file
Posted: 08 Aug 2017 15:43
by smbs
Works great many thanx