I have an HTML file containing block of code which is bounded by predefined text like
#begin#
MYcode line 1
Mycode line 2
some other code
#end#
I would like to run mvn ant command to do processing on this file such that
any text between #begin# to #end# should be replaced by multi block statment i had.
like
#begin#
This is my final code
#end#
Thanks
keshav
<replaceregexp match="#begin#;(.*?)#end#;"
replace="Any Text you want"
flags="gs">
<fileset dir="${project.build.directory}" includes="*.*"/>
Above code will do replacement of regular expression for the tokens.
Related
I am working with stock RHEL7/8 tools, and writing a script that will add a piece to a config file that is formatted as XML. I have run into a case where my sed statement can insert the added text inside a comment.
My current sed command gets the last existence of the tag <Program> and inserts the new tag after its closing tag </Program>.
How can I account for this possibly, but not always being inside a comment?
My script:
sed -i '0,/<Program id/s// <Program id=\"myProgram\"> <\/Program>' filepath
XML Example (displays the error inserting inside comment):
<Program id="myProgram"></Program>
<!--
<Program id="commentedOutProgram"></Program>
<Program id="newlyAddedProgram"><Program>
-->
EDIT:
This is happening at install time. I would like to add a way for some RHEL 7/8 built in tool to look in the XML file, make sure it's not in a comment, and add the new contents
Have a go with this. The usual caveats apply: It probably only works for exactly the sample you provided. Use a proper XML tool if you need a robust solution.
sed -e '/<!--/,/-->/b' \
-e '0,\%<Program id="[^"]*"></Program>%s%<Program id="myProgram"> </Program>%' filepath
Your original script seemed to have several errors, so I couldn't copy it verbatim, but this should at least give you an idea of how to modify it: add a b to skip any lines between <!-- and -->.
The % separators are just to avoid having to backslash slashes; sed allows you to use any separator you like instead of a slash, you just have to backslash the first one.
The b command jumps to a label; if the label is not specified, it jumps to the end of the script, i.e. skips the substitution part and starts over with the next line. The address expression before b selects any comment region, i.e. any lines between a line matching <!-- and a line matching -->.
Quick Summary: I need to create a Bash script to change the text within a node automatically every week. The script will match the node and replace the text inside them (if this is possible)? How would I do this?
Long Summary:
I host a Minecraft server which has shops, each of which have their own .xml file in the /ShowcaseStandalone/ffs-storage/ directory. Every Sunday my server restarts and executes several commands into the terminal to reset several things. One thing that I am trying to make change is one of the shops. I am wanting to change the text in the node <itemstack> and the text in the node <price>. I am simply wanting to take text from a .txt file in a different folder, and insert it into that node. The problem is, that the text in the node will change every week. Is there any way to replace a specific line or text within two nodes using bash?
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<scs-shop usid="cac8480951254352116d5255e795006252d404d9" version="2" type="storage">
<enchantments type="string"/>
<owner type="string">Chadward27</owner>
<world type="string">Frisnuk</world>
<itemStack type="string">329:0</itemStack>
<activity type="string">BUY</activity>
<price type="double">55.0</price>
<locX type="double">487.5</locX>
<locY type="double">179.0</locY>
<locZ type="double">-1084.5</locZ>
<amount type="integer">0</amount>
<maxAmount type="integer">0</maxAmount>
<isUnlimited type="boolean">true</isUnlimited>
<nbt-storage usid="23dffac5fb2ea7cfdcf0740159e881026fde4fa4" version="2" type="storage"/>
</scs-shop>
Operating System: Linux Ubuntu 12.04
You can use xmlstarlet to edit a XML file in a shell like this :
xmlstarlet edit -L -u "/scs-shop/price[#type='double']" -v '99.66' file.xml
NOTE
"/scs-shop/price[#type='double']" is a Xpath expression
see xmlstarlet ed --help
The XML way is cool, but if you need to use normal bash tools, you can modify a line using sed. For instance:
PRICE=123
sed -i "s/\(<price.*>\)[^<>]*\(<\/price.*\)/\1$PRICE\2/" $XML_FILE_TO_MODIFY
This will replace the price with 123.
That sed command seems daunting, so let me break it down:
\(<price.*>\)[^<>]*\(<\/price.*\) is the pattern to match. \( ... \) are parenthesis for grouping. <price.*> matches the opening price tag. [^<>]* matches anything except angle brackets, and in this case will match the contents of the price tag. <\/price.* matches the end of the price tag. Forward slash is a delimiter in sed, so I escape it with a back slash.
\1$PRICE\2 is the text to replace the matched text with. \1 refers to the first matched parenthesis group, which is the opening price tag. $PRICE is the variable with the desired price in it. \2 refers to the second parenthesis group, in this case the closing tag.
I did not have the luxury of having xmlstarlet.
I found a solution though simply by doing an inline replacement;
template-parameter.xml
<ns:Parameter>
<ns:Name required="true">##-ParamName-##</ns:Name>
<ns:Value>
<ns:Text>##-ParamValue-##</ns:Text>
</ns:Value>
</ns:Parameter>
Snippet
tokenName="foo"
tokenValue="bar"
#Replace placeholders in parameter template element
myParamElement=$(cat template-parameter.xml)
myParamElement=${myParamElement//##-ParamName-##/$tokenName}
myParamElement=${myParamElement//##-ParamValue-##/$tokenValue}
Result
<ns:Parameter>
<ns:Name required="true">foo</ns:Name>
<ns:Value>
<ns:Text>bar</ns:Text>
</ns:Value>
</ns:Parameter>
I have a config file which may OR may not have tasks commented out. If the tasks are commented, I want to "uncomment them"!
sed -i '/<!--/d; /-->/d testfile
I have this so far but it removes all the comments in the file.
<!--Comment at the top of the file which should not be removed
-->
<!--
<task>
<tag>
<tag1>keep this content </tag1>
</tag>
</task>
-->
I only want to remove comments for task tags which will look like the above in the file (comment won't be on the same line), but keep the content inbetween. Can i use sed to do this without removing any other comments?
This might work for you (GNU sed):
sed '/^\s*<!--/!b;N;/<task>/s/.*\n//;T;:a;n;/^\s*-->/!ba;d' file
This looks for lines beginning with comments. Then reads in the next line and unless that next line contains the <task> tag it leaves them be. If the next line does contain the <task> tag it deletes the first line, then prints following lines until the closing comment tag which it then deletes.
I am using the following ant script for jdbc compilations.
<sql driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:#10.184.133.133:1521:SUPP"
userid="${UsernameB}"
password="${PasswordB}"
onerror="continue"
delimitertype="row"
delimiter="/"
keepformat="yes">
I have a file with the following content:
create OR REPLACE synonym CIVWS for CIVW;
/
compilation of the above is failing with the following error.
java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
I understand that the delimiter is / and hence the semi colon after the sql statement has caused the issue. There are hundreds of files like this, all will compile properly in sqlplus. However fail with jdbc. I cant change the code now. Is there any work around for this. I cant change the delimiter to ; also. Please suggest.
first you could try to use ... delimiter=";" delimitertype="row" ... and make sure that the pl/sql blocks containing ";"s are separated by some ";" on a single line (with only a newline following it immediately - thus triggering the execution of it as a single statement).
if you can't add the ";" by hand, try to copy the source files to some tmp dir and try to change them there by hand or by some automated regexp replacement (e.g. using ant copy task with a regexp replace filter).
(e.g. replacing lines matching ^(.*end\s*;?)(.*)$ by \1\n;\n\2 could be enough if you are lucky)
another solution for inserting the ";"-single line delimiters could be to put these blocks in separate files (maybe automated) and execute them via the <sql><transaction src="<yoursqlfile>.sql" /> <!--...--> </sql> <!--...-->" ANT task.
I'm trying to make a batch file to pull data out of a file and set it as a variable.
The tricky part is I need to read a XML file, and I only need the data between the quotes of the following line...
narrative="I only need this text here"
The text in that line can also contain spaces, brackets, slashes, dashes and colons.
SAMPLE XML FILE :
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<cadcall>
<call callnumber="123456" jurisdiction="abcd" department="dept 1" complaint="cost" priority="M" calltencode="" callername="Persons Name" phonenumber="Cell Number HERE" narrative="[10/02/2012 14:56:27 : pos9 : PERSON] Fairly long narrative here describing issue of dispatch, but sometimes these can be short." alarmtype="" ocanumber="0000000000" disposition1="TRAN" />
</cadcall>
The proper tool to do this is xmllint from libxml, please, provide a more complete XML example, I will tell you how to use a Xpath request on your XML.
EDIT :
here a solution using Xpath (with a little hack : contains) :
xmllint --xpath '/cadcall/call/#narrative[contains(.,'.')]' file.xml
without seeing the complete input, just based on your example line. grep works for you.
kent$ echo 'narrative="I only need this text here"'|grep -Po '(?<=narrative=")[^"]*'
I only need this text here