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
Search replace .xml file
Moderator: DosItHelp
Re: Search replace .xml file
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
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
Re: Search replace .xml file
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
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
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:
If a single line may have multiple matches, or if you are using the /M option (not shown):
In either case, 'YourReplacement' can be any arbitrary JScript expression that evaluates to your replacement value.
Dave Benham
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
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
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