PowerShell Script to replace lines between strings using RegEx - regex

i wanted to replace what ever lines come between
<? and <Arguments>
and also
</Arguments> and </Task>
via powershell and regex
here's the entire string
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>administrator</Author>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<Enabled>true</Enabled>
<StartBoundary>2013-03-13T00:34:00</StartBoundary>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Settings>
<Enabled>true</Enabled>
<ExecutionTimeLimit>PT259200S</ExecutionTimeLimit>
<Hidden>false</Hidden>
<WakeToRun>false</WakeToRun>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<Priority>5</Priority>
<IdleSettings>
<Duration>PT600S</Duration>
<WaitTimeout>PT3600S</WaitTimeout>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
</Settings>
<Principals>
<Principal id="Author">
<RunLevel>HighestAvailable</RunLevel>
<UserId>SMETEST\Administrator</UserId>
<LogonType>InteractiveTokenOrPassword</LogonType>
</Principal>
</Principals>
<Actions Context="Author">
<Exec>
<Command>C:\Program Files\NetApp\SnapManager for Exchange\SMEJobLauncher.exe</Command>
<Arguments>new-backup -Server 'SME' -ManagementGroup 'Standard' -RetainBackups 8 -RetainUtmBackups 20 -Sto
rageGroup 'testingDB1' -UseMountPoint -MountPointDir 'C:\Program Files\MgrMountPoi
nt' -RemoteAdditionalCopyBackup $False</Arguments>
<WorkingDirectory>C:\Program Files\NetApp\SnapManager for Exchange\</WorkingDirectory>
</Exec>
</Actions>
</Task>
i just want the output to look like
new-backup -Server 'SME' -ManagementGroup 'Standard' -RetainBackups 8 -RetainUtmBackups 20 -StorageGroup 'testingDB1' -UseMountPoint -MountPointDir 'C:\Program Files\MgrMountPoint' -RemoteAdditionalCopyBackup $False

Instead of wasting time on regex with XML - use Select-Xml and XPath:
Select-Xml -Path .\Test.xml -Namespace #{
t = "http://schemas.microsoft.com/windows/2004/02/mit/task"
} -XPath //t:Arguments | foreach { $_.node.InnerText -replace '\n' }
I've remove newlines, assuming that's what you want.

Related

MapForce - Add dayTimeDuration to dayTimeDuration

I am trying to use mapforce to generate an xslt 2.0 file. The mapping is adding 2 dayTimeDuration elements, doing so results in the following error;
No match for core.add(xs:dayTimeDuration, xs:dayTimeDuration). Check argument types.
Supported: +(xs:double, xs:double) -> xs:double
I thought that xslt 2.0 supported adding 2 dayTimeDurations. Is there a way of doing this using mapforce?
Cheers
Stew
Had almost the same problem, first tried to add functx-library but saw it creates absolute path in the generated xslt2-code, which isn't very good.
Well, turns out you can implement that function, but first you have to do some modifications...
Find your Mapforce installation directory, and MapForceLibraries -subdirectory. From that open the "core.mff", and find
<group name="math functions">
<component name="add" growable="true" growablebasename="value">
<sources>
<datapoint name="value1" type="xs:decimal"/>
<datapoint name="value2" type="xs:decimal"/>
</sources>
<targets>
<datapoint name="result" type="xs:decimal"/>
</targets>
As you can seem the "sources" and "targets" elements seems to define the in- and out data types. As it is, they have only implemented "add"-function for "xs:decimal". You can copy/paste this component, then rename it and give new in- out- data types, in your case they are both "xs:dayTimeDuration". Note that there are implementations for each supported language, but you can omit those that are not needed. Here's
what should work:
<component name="addDayTimeDuration" growable="true" growablebasename="value">
<sources>
<datapoint name="value1" type="xs:dayTimeDuration"/>
<datapoint name="value2" type="xs:dayTimeDuration"/>
</sources>
<targets>
<datapoint name="result" type="xs:dayTimeDuration"/>
</targets>
<implementations>
<implementation language="xslt">
<operator value="+"/>
</implementation>
<implementation language="xslt2">
<operator value="+"/>
</implementation>
<implementation language="builtin">
<function name="Core_Add"/>
</implementation>
</implementations>
<description>
<short>result = value1 + value2</short>
<long>Result is the dayTimeDuration value of adding value1 and value2.</long>
</description>
</component>
Your new function should now appear in the "math functions" and should be good to use.
After contacting Altova (the makers of MapForce);
While XPath 2 does offer a subtract-dayTimeDurations operation, this is not presently offered as a function inside MapForce.

Unable to add new tags in xml using sed in linux shell [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 8 years ago.
I am trying to add an xml element in my "pom.xml" file at a particular place, i have tried as the answer mentioned in the following question. But still it is not working..
I do not understand REGEX , so i am having trouble figuring out the problem.
I have to add following xml element between the "properties" and "dependencies" tag.
<distributionManagement>
<repository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://your.server.com:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
"pom.xml (Before)"
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Component</artifactId>
<version>1.0.0</version>
<packaging>carbon/application</packaging>
<properties>
....
</properties>
<dependencies>
<dependency>
<groupId>com.example.proxy-service</groupId>
<artifactId>Handler</artifactId>
<version>1.0.0</version>
<type>xml</type>
</dependency>
</dependencies>
</project>
pom.xml(After)
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Component</artifactId>
<version>1.0.0</version>
<packaging>carbon/application</packaging>
<properties>
....
</properties>
<distributionManagement>
<repository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://your.server.com:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>com.example.proxy-service</groupId>
<artifactId>Handler</artifactId>
<version>1.0.0</version>
<type>xml</type>
</dependency>
</dependencies>
</project>
Shell Script:
#!/bin/sh
# First Script
CONTENT="<distributionManagement>\n<repository>\n<id>deployment</id>\n</repository>\n</distributionManagement>"
echo "Start"
C=$(echo $CONTENT | sed 's/\//\\\//g')
sed "/<\/project>/ s/.*/${C}\n&/" *pom.xml*
echo "finish"
Error i am getting:
sed: -e expression #1, char 43: unterminated `s' command
Please help me that if i am following the right command or approach. If there is another command that will do the job but it should be supported both in "Ubuntu" and "Redhat".
I would suggest
#!/bin/bash
CONTENT=" <distributionManagement>
<repository>
<id>deployment</id>
</repository>
</distributionManagement>"
awk -v content="$CONTENT" '{ print $0 } /<\/properties>/ { print content }' pom.xml
This instructs awk to print all lines, and additionally print content (here set to "$CONTENT") if the current line matches </properties>. Generally, I find that if you're tempted to substitute shell variables into sed commands, it's worth considering awk instead. Others will probably disagree.

shell scripting using regular expression pattern matching

I have a file say abc.txt which contains..
<mapping number=1 name=m1>
<transformation type=aggregator name=agg_m1> />
<transformation type=joiner name=j_m1 />
</mapping>
<mapping number=2 name=m2>
<transformation type=router name=rtr_m2> />
<transformation type=joiner name=j_m2 />
</mapping>
I neen an output of the form of the same order. i.e, i need to get name field from mapping and type and name fields from transformation "in the same order :
name=m1
type=aggregator name=agg_m1
type=joiner name=j_m1
name=m2
type=router name=rtr_m2
type=joiner name=j_m2
I have used the command 'awk' to get the output , but I am getting it of the following form which is incorrect :
name=m1
name=m2
type=aggregator name=agg_m1
type=joiner name=j_m1
type=router name=rtr_m2
type=joiner name=j_m2
I tried using 'grep' and 'sed' commands too but I am getting the desired output, because the order is not preserved. I need to the get the output where the order is preserved.
perl -lne '$_=~m/^[^\s]*\s([^\/\>]*)/g;print $1' your_file
tested below:
> cat temp
<mapping number=1 name=m1>
<transformation type=aggregator name=agg_m1> />
<transformation type=joiner name=j_m1 />
</mapping>
<mapping number=2 name=m2>
<transformation type=router name=rtr_m2> />
<transformation type=joiner name=j_m2 />
</mapping>
> perl -lne '$_=~m/^[^\s]*\s([^\/\>]*)/g;print $1' temp
number=1 name=m1
type=aggregator name=agg_m1
type=joiner name=j_m1
number=2 name=m2
type=router name=rtr_m2
type=joiner name=j_m2
>
If you do not need number,then use the below regex:
perl -lne '$_=~m/^[^\s]*\s[number=\d]*([^\/\>]*)/g;print $1' your_file
Here's one way using grep:
grep -oE "(name|type)=[^ >]* *[^ >]*" file
Results:
name=m1
type=aggregator name=agg_m1
type=joiner name=j_m1
name=m2
type=router name=rtr_m2
type=joiner name=j_m2

MSBuild RegexMatch not matching

I have the following
<RegexMatch Input="$(Configuration)" Expression="^.*?(?=\.)">
<Output ItemName="Theme" TaskParameter="Output" />
</RegexMatch>
My configuration variable is as follows Theme.Environment
So "Default.Debug"
or "Yellow.Release"
I would like to get the first portion in to a varaible called theme.
I have tested this regex and it works in stand alone regex testers
^.*?(?=\.)
but not when used in my build file.
I am echoing the variable out so that i can see the output
<Exec Command="echo $(Theme)"/>
<Exec Command="echo $(Configuration)"/>
Ideas?
If you should use MSBuild Community tasks for that - check this line: <Output PropertyName="Theme" TaskParameter="Output" />
you should use PropertyName="Theme" if you want to refer it like $(Theme) later.
ItemName will create items set, not property.
But it's much simplier to use MSBuild 4.0 inline functions than Msbuild community tasks for that concrete task. Your code will looks like this (adopt for your script):
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTarget="Play">
<PropertyGroup>
<Configuration>Yellow.Release</Configuration>
</PropertyGroup>
<Target Name="Play">
<PropertyGroup>
<Theme>$([System.Text.RegularExpressions.Regex]::Match($(Configuration), `^.*?(?=\.)`))</Theme>
</PropertyGroup>
<Message Text="$(Theme)" />
<Message Text="$(Configuration)" />
</Target>
</Project>
Just realised that RegexMatch doenst return the matched string but rather returns the entire string if matched.
basically it called IsMatch method not Match method
Have re written as a RegexReplace
<RegexReplace Input="$(Configuration)" Expression="\..*" Replacement="" Count="1">
<Output ItemName="Theme" TaskParameter="Output" />
</RegexReplace>
After that it still wasnt working and then i realised i was doing
$(Theme)
Should have been
#(Theme)

Syntax mode for Coda not working - Regex issue

I have the following SyntaxDefinition.xml file that I am using to create syntax highlighting for SilverStripe(.ss) files. However, I get a regex error with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE syntax SYSTEM "syntax.dtd">
<syntax>
<head>
<name>SilverStripe Syntax</name>
<charsintokens><![CDATA[_0987654321abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#]]></charsintokens>
</head>
<states>
<default id="Base" color="#000">
<state id="String" color="#760f15">
<begin><regex>"</regex></begin>
<end><regex>(((?<!\\)(\\\\)*)|^)"</regex></end>
</state>
<state id="Variable" color="#ff0000">
<begin><regex>^\$([a-z])(?:)</regex></begin>
<end><regex>[\n\r]</regex></end>
</state>
<import mode="PHP-HTML"/>
</default>
</states>
</syntax>
I want the "Variable" part of this code to color any code starting with a dollar sign, e.g. $Content.
Try:
<begin><regex>^\$[^\r\n]+</regex></begin>
or
<begin><regex>^\$</regex></begin>
depending on how it works
I found a bit of code which seems to work:
<regex>(\$([\w\d])+)</regex>