Adding the xml tag after the specified string using SED - sed

Planning to add the below content after the closing tab into the main.xml
Keeping the below xml tag information into the ADD.txt file.
<student>
<name>Bob</name>
<id>0111</id>
-----
</student>
Executing the following command: The information is adding above the tag </class>,
Planning to add the ADD.txt content after the tag </class>
sed -i '/</class>/e cat ADD.txt' main.xml

Related

How to use OPC UA-ModelCompiler with multiple xml model input files?

I've used succesfully UA-ModelCompiler from OPCFoundation (https://github.com/OPCFoundation/UA-ModelCompiler) for compiling my xml model, using the following format:
OPC.UA.ModelCompiler.exe -d2 ".\MyOpcUaBasic.xml" -cg ".\MyOpcUaBasic.csv" -o2 ".\MyOutputFolder"
I'd like to compile another OPC UA xml model that uses more than one namespace declared at the beginning of the model.
Example:
<Namespaces>
<Namespace Name="MyOpcUaBasic" Prefix="Opc.Ua.Basic" XmlPrefix="MyOpcUaBasic">http://Myorganization.it/Basic/"</Namespace>
<Namespace Name="MyOpcUaBasic2" Prefix="Opc.Ua.Basic2" XmlPrefix="MyOpcUaBasic2">http://Myorganization.it/Basic2/"</Namespace>
<Namespace Name="OpcUa" Prefix="Opc.Ua" XmlPrefix="OpcUa" XmlNamespace="http://opcfoundation.org/UA/2008/02/Types.xsd">http://opcfoundation.org/UA/</Namespace>
</Namespaces>
So I need more than one xml input to generate my xml nodeset.
I have tried the following:
OPC.UA.ModelCompiler.exe -d2 ".\MyOpcUaBasic.xml" -cg ".\MyOpcUaBasic.csv" -d2 ".\MyOpcUaBasic2.xml" -cg ".\MyOpcUaBasic2.csv" -o2 ".\MyOutputFolder"
But it does not work giving an error.
How should I do?
It is possible the usage is not right, but I have't found how to use more than one input xml to create my nodeset, if this is possible.
Thanks.
Found the solution, I forgot the FilePath field
<Namespace Name="MyOpcUaBasic" Version="1.00" PublicationDate="2020-05-27T00:00:00Z" Prefix="Opc.Ua.SmBasic" XmlPrefix="MyOpcUaBasic" XmlNamespace="http://Myorganization.it/OpcUa/ServicesModel/Basic/Types.xsd" FilePath="MyOpcUaBasic">http://Myorganization.it/Basic/</Namespace>

How do you comment out an XML node using Powershell 3.0?

I would like to comment out an XML node in a configuration file using Powershell 3.0.
For instance, if this is my config.xml file:
<node>
<foo type="bar" />
</node>
I would like my script to change the file to this:
<node>
<!-- <foo type="bar" /> -->
</node>
I am looking to do this using Powershell 3.0's native XML/XPATH functionality, and not match/regex-based string replacement.
Use CreateComment() to create a new comment node containing the existing node's XML, then remove the existing:
$xml = [xml]#'
<node>
<foo type="bar" />
</node>
'#
# Find all <foo> nodes with type="bar"
foreach($node in $xml.SelectNodes('//foo[#type="bar"]')){
# Create new comment node
$newComment = $xml.CreateComment($node.OuterXml)
# Add as sibling to existing node
$node.ParentNode.InsertBefore($newComment, $node) |Out-Null
# Remove existing node
$node.ParentNode.RemoveChild($node) |Out-Null
}
# Export/save $xml

Search and Replace within XML tag via sed over multiple lines

i want to change the value of a specific XML tag property.
There are many questions about how to handle sed, but the problem here is the newline within the tag.
I want to change the value after name= and it must be searched in the <package ... > tag
XMLStarlet is not an option.
Coverage.xml
<package branch-rate="0.031746031746" complexity="0.0"
line-rate="0.159420289855" name="include">
<classes>
<class branch-rate="0.0" complexity="0.0"
My best try so far:
sed -n '/<package/ {
:a
n
/<classes>/q
s/name=/xxxx/g
}' coverage.xml
Do you have an idea?
UPDATE 2: More of coverage.xml with approach of #RavinderSingh13
<package branch-rate="0.031746031746" complexity="0.0"
line-rate="0.159420289855" name="NEW_VALUE">
<classes>
<class branch-rate="0.0" complexity="0.0"
filename="NEW_VALUE"
name="NEW_VALUE">
If you are ok with awk, then as per your shown samples could you please try following once.(this will look only for package tag and its name value and for rest tags it will not do anything)
awk '/^>/{flag=""} /<package/{flag=1} flag && /name=/{sub(/name=.*\"/,"name=\"NEW_VALUE\"")} 1' Input_file
In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file to above code too.
I just did a little tweak using a for loop.
LINES=`awk '/<package /{print NR+1}' coverage.xml`
for i in ${LINES};
do
echo $i
sed -i ''"${i}"'s/name=.*/name="NEW_VALUE"/' coverage.xml;
done
The NR+1 helps to reach the second line in the package tag.

In Magento2 how to create custom less file for particular store?

In Magento2, how to create custom.less file for a particular store ?
i created custom themes like theme1, theme2 and theme3 in magento2
and also i created custom.less file only in theme but when i hit the command to compile this less file but is generated in all themes folders
i am only create this custom.less file in theme
these steps i follow to compile
step 1 : i am created custom.less file
step 2 : i am run these command to generate from custom.less to csutom.css
- php bin/magento setup:static:content:deploy -f
- php bin/magento cache:clean
conculsion : but this custom.css file generated in all themes.
i have created multistore websites. i have created custom.less file in first store theme but after executed all the commands css file is generated in all stores.
Working with LESS in magento 2 specific store
step1 : Go in your custom theme > web > css > source > there is _soucrces.less
file if it is not there then copy this file from magento-blank-theme.
step2 : After copied that file type code for import your file eg. <#import 'CustomCss.less';> here CustomCss.less is your file name
step3: now create one less file in following path ( web > css > source > CustomCss.less ) yourfile name name starts should be underscore ( CustomCss )
step4 : Now declare your custom variables in _variables.less file , you can find this file in following path (web > css > source )
file if it is not there then copy this file from magento-blank-theme. ( optional step ) or you can declare variables in same LESS file
step5 : After creating this files write css rules and also use variables which you are created.
step5: in your theme default_head_block.xml
add your css file in head section
result: code which in the less files it is added in customCss.css

Search xml for a value using sed

I have a below xml file
<documents>
<document><title>some title1</title><abstract>Some abstract1</abstract></document>
<document><title>some title2</title><abstract>Some abstract2</abstract></document>
<document><title>some title3</title><abstract>Some abstract3</abstract></document>
<document><title>some title4</title><abstract>Some abstract4</abstract></document>
</documents>
I am trying to write a ksh script to fetch the abstract value based on title=title4
xmllint , xstartlet is not allowed in my machine (access issues)
I have tried with
sed -n '/abstract/{s/.*<abstract>//;s/<\/abstract.*//;p;}' connections.xml
How to modify this to search based on a title
Based on the example you have given:
sed -n '/title>.*title4<\/title>/{s#.*<abstract>##;s#</abstract>.*##;p}' file
Will give you:
Some abstract4
grep approach:
grep -Poz '<title>.*?title4</title><abstract>\K[^<>]+(?=</abstract>)' connections.xml && echo ""
The output:
Some abstract4