I want to run a shell script with this usage:
./run A.txt B.xml
A.txt contain some statistic:
Accesses = 1
Hits = 2
Misses = 3
Evictions = 4
Retries = 5
B.xml looks like:
<stat name="total_accesses" value="0"/>
<stat name="total_misses" value="0"/>
<stat name="conflicts" value="0"/>
I want to replace some stats in B.xml from A.txt. For example, I want to
1- find "Accesses" in A.txt
2- find "total_accesses" in B.xml
3- replace 0 with 1
1- find "Misses" in A.txt
2- find "total_misses" in B.xml
3- replace 0 with 3
So B.xml will look like:
<stat name="total_accesses" value="1"/>
<stat name="total_misses" value="3"/>
<stat name="conflicts" value="0"/>
I want to do that with shell “sed” command. However I find it quite complex as the regexp is hard to understand.
Does “sed” help me in this problem or I have to find another way?
It might be a bit heavy-weight for such a simple case, but here’s a Python script that does the job:
Example
To process input files incrementally or to makes changes inplace you could use the following:
Example
It can read from stdin or process several files: