I have this file config.xml
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>PASSWORD</enter>
<content src="index.html" />
<access origin="*" />
I tried to do it with sed without success.
I need to do this:
$./script.sh config.xml NEWPASSWORD
to get:
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>NEWPASSWORD</enter>
<content src="index.html" />
<access origin="*" />
Using backreference:
sed "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
^\( *<enter>\): search for lines starting with any number of spaces followed by <enter>. Matching characters are captured with escaped parentheses.
\([^>]*\)<: following characters up top next < are captured in a second group.
\1$2<: in the substitution string, characters from first group are output(\1) followed by the second parameter value passed to the script, ($2, the new password value)
The command is applied to $1, the file passed as first parameter to the script (the file name).
To edit the file in place, use the -i flag:
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
The good result is:
$cat script.sh
#!/bin/sh
file=$1
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
Then:
$./script.sh config.xml NEWPASSWORD
Many thanks to everyone, especially to Kenavoz.
Related
Is there a command (using sed or any other tool) to remove XML comment (which has a comment string also) on some XML tags? I am using centos 7.4 and 7.9. The XML structure is like this:
<root>
<!-- Some string here to inform
<SomeTagHere />
<SomeTagHere2> </SomeTagHere2>
-->
</root>
I tried many different sed commands but none of them worked and I couldn't find a proper regex for this. What I want to is release tags inside comment like this:
<root>
<SomeTagHere />
<SomeTagHere2> </SomeTagHere2>
</root>
I am trying to get the source files and include directories from a vcxproj file. Eg of vcsproj:
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
....
<ItemGroup>
<ClCompile Include="$(SrcDir)d1\f1cpp" />
<ClCompile Include="$(SrcDir)d2\f2.cpp" />
</ItemGroup>
...
</Project>
I tried this:
xmlstarlet sel -t -v "//_:ItemGroup/ClCompile/#Include" myProj.vcxproj but didn't work.
However, when I tried this (copying the code from some page that I came across), it works:
echo '<?xml version="1.0" encoding="utf-8"?> <ELEMENT xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<SUB_ELEMENT attribute="attr_value"/>
</ELEMENT>' | xmlstarlet sel -t -v '//_:SUB_ELEMENT/#attribute' --nl
o/p: attr_value
I don't see how the two are different with respect to reading an attribute value from an xml with a namespace. I further tried a stripped down version of the vcxproj file:
echo '<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="$(SrcDir)d1\f1cpp" /> </ItemGroup> </Project>' | xmlstarlet sel -t -v '//_:ItemGroup/#Include' --nl
o/p: <no o/p>
Any indication on why it is not working or how to get this to work would be very helpful.
Edit: Expected output from the vcxproj would be a list of filenames. For the above command it would be $(SrcDir)d1\f1cpp
Any indication on why it is not working or how to get this to work would be very helpful
Since you're using the default namespace add the _:
shortcut on the node test in each
location step,
and the -T (--text)
option to make text mode output:
xmlstarlet select -T -t -v "//_:ItemGroup/_:ClCompile/#Include" -n file.xml
I am trying to insert a string path stored in a variable into an xmlfile as the first part of an attribute value.
The xmlfile is a context.xml file for Tomcat using the form
<Context reloadable="true" docBase="application.war">
...
</Context>
A path, which is the absolute location of a war file to deploy is
$WKSP_APATH="/location/on/server
$WKSP PATH should be inserted as follows
<Context reloadable="true" docBase="/location/on/server/application.war">
...
</Context>
I've tried to find information, but most sources focus on full line insertion.
This is the last line to add for my script.
For context the executed line location is given below as an extract from the script it is embedded in
find /$SEARCH_PATH -name "*.xml" -type f|while read fname; do
CONTEXT_FILE=${fname##*/}
cp -rf "/$SEARCH_PATH/$CONTEXT_FILE"
"/$COPY_LOCATION/${PROJECT_CODENAME}-$CONTEXT_FILE"
echo "Copied $CONTEXT_FILE to:
$COPY_LOCATION/$PROJECT_CODENAME-$CONTEXT_FILE for deployment"
#do insert new path on local server here?
done
Many thanks
Assuming that:
You take the risk to parse xml files using regex instead of proper tools.
The FILE variable contains the xml filename to process.
The WKSP_PATH variable contains the absolute location you want to insert as a prefix in the value of the docBase attribute.
Try:
#!/bin/bash
WKSP_PATH="/location/on/server"
sed -i "s#docBase=\"#docBase=\"$WKSP_PATH/#" "$FILE"
To check the result:
cat "$FILE"
<Context reloadable="true" docBase="/location/on/server/application.war">
...
</Context>
we need to add the line <property name="assumeHungTime" value="600000" />
after the line <bean id="DataUnpacker" class="com-packer" singleton="false">
in one xml file . Please let me know the command to add it - using sed?
Thanks,
Ravikanth
The follwing sed command will do what you want.
sed -e '/<bean id="DataUnpacker" class="com-packer" singleton="false">/a<property name="assumeHungTime" value="600000" />'
I need to replace a particular character in a text file with another character. For example, replacing "E" with "A":
Apple ice → ApplA Ica
While executing sed 's/E/A' < apple.txt > app.txt I receive the error
function cannot be parsed
Please help! I need to automate this using Antscript.
You should terminate your sed command with a slash (/) and I guess you want to exchange all occurences of E with A? Then you have to add a g for a global substitution:
sed 's/E/A/g' app.txt
sed 's/E/A/g' app.txt. You missed the trailing / (g means all occurrences),
Since you are in Ant environment, you probably don't need to execute sed at all, but rather use Copy task with filter, or ReplaceRegExp task.
<?xml version="1.0" encoding="UTF-8"?>
<project>
<replaceregexp file="apple.txt" flags="g" match="e" replace="A"/>
</project>
This alters the file in place:
$ cat apple.txt
Apple ice
$
$ ant
Buildfile: build.xml
BUILD SUCCESSFUL
Total time: 0 seconds
$
$ cat apple.txt
ApplA icA
Your example is strange with case (a|A, e|E). I'll assume that's typo.
Follow up: To declare encoding...
<?xml version="1.0" encoding="utf-8"?>
<project>
<replaceregexp file="apple.txt" encoding="utf-8" flags="g" match="Á" replace=" "/>
</project>
I tested this successfully. Before:
ApplA icA
ApplÁs icÁs
After:
ApplA icA
Appl s ic s