Replacing string with variable with Groovy and SED command - linux

I am trying to replace an entire description string contained in an XML file. I would like to replace that string with a variable. I am using a SED command within a Groovy script.
I have the following code. I am expecting the string "foo" to replace the description text but it doesn't.
Instead the following line causes the XML to change to:
Description="sDescription"
What am I doing wrong?
def sDescription = "foo"
def sedCommand = 'sed -i \'s/Description="[^"]*"/Description="'$sDescription'"/g\' package.appxmanifest' as String

In Groovy variable/expression substitution inside of strings (interpolation) only works with certain types of string literal syntax. Single quote syntax ('content') is not one of them. However, if you replace the outer single quotes with double quotes ("content") then you should get the interpolation effect you are looking for:
def sDescription = "foo"
def sedCommand = "sed -i 's/Description=\"[^\"]*\"/Description=\"$sDescription\"/g\' package.appxmanifest" as String
This should give you the string that contains the command you wish to run. Please note how I changed the special character escaping (\) within the string to reflect the change in string delimiters.
Aside: As noted by #tim_yates, Why would you want to invoke a separate ad hoc process to do this substitution when Groovy contains excellent XML manipulation facilities built into the language?

Related

Groovy: treat a string with interpolation placeholder as a regular string and don't interpolate

We're using a third party tool that uses groovy. We have two steps:
Construct a string dynamically -> ${p:test},${p:test2}
We interpolate the string which got constructed in the first step
However, no matter what I try, as soon as groovy detects ${, it treats if for interpolation. How can one get groovy to output '${p:test},${p:test2}' as a string without interpolating the values?
I tried so many things:
Escaping with \
Slashy strings
StringBuilder
<<=
Dollar slashy string
As long as I remove the '{' from the string, it is handled like a string. Even doing something like '$#p:test},$#p:test2}'.replace('#', '{') results in interpolation.

Escaping characters in cucumber step definition

I have used following step def for capture error message from given pop up modal
Then warning "Invalid file. File name should be "Users.xml" " should be given to user
In this case I want to include "User.xml" inside the given string
#Then("^warning \"([^\"]*)\" should be given to user
But this will not escaping the ". How do I escape this " character for matching the exact string
You need to change
#Then("^warning \"([^\"]*)\" should be given to user
to
#Then("^warning \"(.*?)\" should be given to user$"
[^"]* is matching everything except quotes
.*? will match everything (including quotes )
For more information See here
You can also use multiline string to do that as explained in above link
Also see this video tutorial for including strings in your arguments

Python-String to function in 'eval'

I'm trying to build a lisp interpreter in python using 'eval'.
def FUNC(e1,*elements):
print(e1)
eval((input()))
Its working for integers.But is there any way I can use this to input strings?
eg: when I input:
F(a,b,3,5,2)
The above code should print:
a
I don't want to input it as: F('a','b',3,5,2)
Thanks!
The only way to convey a string value is by enclosing it within quote. Other way to do it is by declaring a string variable separately and send it as a parameter instead. However, when you print a string query in interactive command window, it won't be printed with quotes aroun

Make substitution reference on strong containing equals sign

I'm trying to use a Make substitution reference to alter a string. The problem being that the string happens to contain an equals = symbol.
For example:
INPUT = -switch1 -switch2=potato -switch3
OUTPUT = $(INPUT:-switch2=%=-switch2=turnip)
all:
#echo TEST : $(OUTPUT)
so in the form $(var:a=b), INPUT is var, -switch2=% is a and -switch2=turnip is b.
Obviously that doesn't work because = is a special character in this context, but I've no idea how to make it realize that this is part of string a.
I've tried quoting, backslashes, alternative escape characters and putting -switch2=% in a variable and using that instead. All to no avail.
I know I can use patsubst, but that'd be adding the first non-POSIX extension to the file and i'd prefer to not be that guy.
Any suggestions appreciated!
You'll have to use a full patsubst function. Substitution references are just a shortcut for patsubst:
OUTPUT = $(patsubst -switch2=%,-switch2=turnip,$(INPUT))

Unterminated string in AutoIT

I like to make a script in AutoIT to work in Excel. when i trying to make a variable with the following argument is giving me error.
$myformula = "=IF(OR(ISNUMBER(search(" "&{"quality","st","rainbow","carryout","auto","surgery","public","fertiliser","Fertilisers","ifertilisersl","rendezvous","remember","that","precast","barefoot","mail","scaffolding","safe","access","salvation","army","funeral","directors","director","seahorse","simply","affordable","stich","time","dressmaking","alterations","hospital","by","jewellers","post","sweet","sensation","trophies","engraving","protection","produce","public","relations","made","fun","auctions","auction","kiltmakers","kiltmaker","linen","chippy","timber","tidy","tangles","past","dealer","trading","crafts","craft","naval","activities","US","Uncle","Diner","Removals","Volunteer","Zoo","Wavelength","Wavelengths","Friends","hairdressing","theatreclub","daynursery","MUSLC","superstore","colleges","AALCO","AARDVARK","ABACUS","ABB","ABBOTSWELL","ABBOTTPUBLIC","ABC","ABCON","ABDN","ABERCARE","ABERDEE","ABERDEEN","ABERDEENSHIRE","ABERFLORA","ABERGELDIE","ABERGLEN","ABERLOUR","ABERMED","ABERNE","ABERPEST","ABERSOL","ABERT","ABSCOT","ABSOFT","ABSPEK","ABSURE","ABTEX","ABTRUST","ACADEMY","ACCESS","ACCESSORIES","ACCOMMODATION","ACCORD","ACCOUNTANCY","ACCURAY","ACE","ACTION","ADAPT","ADEPT","ADMIN","ADMIRAL","ADS","ADULT","ADVANCE","ADVANCED","ADVANT","ADVANTAGE","ADVICE","AEA","AFFAIR","AFOS","AGA","AGE","AGENCY","AGIP","AGRI","AGRICULTURAL","AGRICULTURE","AHT","AI","AIDS","AIR","AIRBORNE","AIRFAIR","AIRLINES","AIRNAUTIC","AIRPAC","AIRPORT","AIRWAYS","AIRYHALL","AIRYLEA","AKRON","ALAD","ALASDAIR","ALB","ALBATECH","ALFA","ALL","ALLIANCE","ALLIED","ALLOMAX","ALPINE","ALSTEV","ALTERNATIVES","ALTRA","ALWAYS","ALZHEIMER","AM","AMALGAMATED","AMAT","AMATOLA","AMBER","AMEC","AMERICAN","AMETEK","AMIN","AMITEC","AMOCO","AMTECH","AMTRAK","AMUSEMENT","AND","ANDERGAUGE","ANIXTER","ANKA","ANTIQUES","ANTRIM","AOC","AP","APARDION","APEX","APOLLO","APOSTOLIC","APPEALS","APPLIED","APPOLLO","AQUA","AQUARISTS","AQUATEC","AQUATIC","AQUIDATA","ARCHITECTURAL","ARCO","ARGONAUT","ARGOSY","ARISTACUT","ARJO","ARJON","ARKAIG","ARTISTES","ARTISTS","ARTWORKS","ARUP","AS","ASA","ASCOT","ASDASUPERSTORES","ASHVALE","ASPECT","ASSEMBLIES","ASSESSORS","ASSOCIATED","ASSOCIATES","ASSOCIATION","ASSURANCE","ASTER","ASTRA","ATEL","ATHENA","ATHENAEUM","ATHOLL","ATLANTIC","AUCHMILL","AUCHTERLESS","AUGHTON","AUGUST","AUP","AUQUHARNEY","AURORA","AUTO","AUTOFIX","AUTOGLASS","AUTOMAGIC","AUTOQUICK","AUTOSAVE","AUTOSPRAY","AUTOTRUCK","AVCO","AVENUE","AWAKENING","AWAY","AXIOM","AZTEC","BAADER","BALNAGASK","BANKHEAD","BAR","BARICO","BARRIER","BARTERING","BASELINE","BATCHBRIGHT","BATH","BATHROOM","BAVARIA","BAYLISS","BAYLOR","BEARING","BEATTIES","BEAUTY","BEAVERBROOKS","BEECHWOOD","BEEFEATER","BEELINE","BEFRIEND","BENBOW","BESTWAY","BEWS","BIKES","BLIND","BLINDCRAFT","BLINDS","BLOOD","BLOOMING","BLOSSOMS","BLOW","BLUEBELLS","BMT","BNA","BOARD","BOARS","BOAT","BODY","BOHEMIA","BOILERMAKERS","BON","BONADDIO","BONAVENTURE","BONIVIEW","BONNYMUIR","BOOKBINDING","BOOTS","BOROWSKI","BORSALINO","BOSANQUET","BOSCH","BOTTLED","BOULEVARD","BOURTREE","BOWEN","BOWLING","BOWTECH","BOX","BP","BPCC","BRA","BRACKENRIDGE","BRAEHEAD","BRAESIDE","BRAKE","BRIEF","BRITANNIA","BRITANNIC","BROAD","BROADCAST","BROADSTRAIK","BROS","BROTHERHOOD","BRUNSWICK","BUCKSBURN","BUDGET","BUILDING","BULAWEYO","BUREAU","BURNTHILLS","BUS","BUSINESS","CABER","CABINS","CABLE","CABS","CAFE","CAIRD","CAIRNCRY","CAIRNGORM","CAKE","CALEDON","CALEDONIA","CALEDONIAN","CALLANDERS","CALOR","CALTEC","CALVARYTABERNACLEUNITED","CAMARGUE","CAMEO","CAMPAIGN","CAMPBELLS","CAMPHILL","CAMWATER","CAN","CANALE","CANCER","CANDO","CANSCO","CAPE","CARADON","CARD","CAREERS","CARGO","CARPET","CARPETS"}&" "," "&b1&" "))),"YES","NO")"
I tried with double quotes as well. What shall /i do?
Strings are enclosed in double-quotes like "this". If you want a string to actually contain a double-quote use it twice like:
"here is a ""double-quote"" - ok?"
You can also use single-quotes like 'this' and 'here is a ' 'single-quote' ' - ok?'
You can mix quote types to make for easier working and to avoid having to double-up your quotes to get what you want. For example if you want to use a lot of double-quotes in your strings then you should use single-quotes for declaring them:
'This "sentence" contains "lots" of "double-quotes" does it not?'
is much simpler than:
"This ""sentence"" contains ""lots"" of ""double-quotes"" does it not?"
Just use ' instead of ".
Like this:
$myformula = '=IF(OR(ISNUMBER(search(" "&{"quality","st","rainbow","carryout","auto","surgery","public","fertiliser","Fertilisers","ifertilisersl","rendezvous","remember","that","precast","barefoot","mail","scaffolding","safe","access","salvation","army","funeral","directors","director","seahorse","simply","affordable","stich","time","dressmaking","alterations","hospital","by","jewellers","post","sweet","sensation","trophies","engraving","protection","produce","public","relations","made","fun","auctions","auction","kiltmakers","kiltmaker","linen","chippy","timber","tidy","tangles","past","dealer","trading","crafts","craft","naval","activities","US","Uncle","Diner","Removals","Volunteer","Zoo","Wavelength","Wavelengths","Friends","hairdressing","theatreclub","daynursery","MUSLC","superstore","colleges","AALCO","AARDVARK","ABACUS","ABB","ABBOTSWELL","ABBOTTPUBLIC","ABC","ABCON","ABDN","ABERCARE","ABERDEE","ABERDEEN","ABERDEENSHIRE","ABERFLORA","ABERGELDIE","ABERGLEN","ABERLOUR","ABERMED","ABERNE","ABERPEST","ABERSOL","ABERT","ABSCOT","ABSOFT","ABSPEK","ABSURE","ABTEX","ABTRUST","ACADEMY","ACCESS","ACCESSORIES","ACCOMMODATION","ACCORD","ACCOUNTANCY","ACCURAY","ACE","ACTION","ADAPT","ADEPT","ADMIN","ADMIRAL","ADS","ADULT","ADVANCE","ADVANCED","ADVANT","ADVANTAGE","ADVICE","AEA","AFFAIR","AFOS","AGA","AGE","AGENCY","AGIP","AGRI","AGRICULTURAL","AGRICULTURE","AHT","AI","AIDS","AIR","AIRBORNE","AIRFAIR","AIRLINES","AIRNAUTIC","AIRPAC","AIRPORT","AIRWAYS","AIRYHALL","AIRYLEA","AKRON","ALAD","ALASDAIR","ALB","ALBATECH","ALFA","ALL","ALLIANCE","ALLIED","ALLOMAX","ALPINE","ALSTEV","ALTERNATIVES","ALTRA","ALWAYS","ALZHEIMER","AM","AMALGAMATED","AMAT","AMATOLA","AMBER","AMEC","AMERICAN","AMETEK","AMIN","AMITEC","AMOCO","AMTECH","AMTRAK","AMUSEMENT","AND","ANDERGAUGE","ANIXTER","ANKA","ANTIQUES","ANTRIM","AOC","AP","APARDION","APEX","APOLLO","APOSTOLIC","APPEALS","APPLIED","APPOLLO","AQUA","AQUARISTS","AQUATEC","AQUATIC","AQUIDATA","ARCHITECTURAL","ARCO","ARGONAUT","ARGOSY","ARISTACUT","ARJO","ARJON","ARKAIG","ARTISTES","ARTISTS","ARTWORKS","ARUP","AS","ASA","ASCOT","ASDASUPERSTORES","ASHVALE","ASPECT","ASSEMBLIES","ASSESSORS","ASSOCIATED","ASSOCIATES","ASSOCIATION","ASSURANCE","ASTER","ASTRA","ATEL","ATHENA","ATHENAEUM","ATHOLL","ATLANTIC","AUCHMILL","AUCHTERLESS","AUGHTON","AUGUST","AUP","AUQUHARNEY","AURORA","AUTO","AUTOFIX","AUTOGLASS","AUTOMAGIC","AUTOQUICK","AUTOSAVE","AUTOSPRAY","AUTOTRUCK","AVCO","AVENUE","AWAKENING","AWAY","AXIOM","AZTEC","BAADER","BALNAGASK","BANKHEAD","BAR","BARICO","BARRIER","BARTERING","BASELINE","BATCHBRIGHT","BATH","BATHROOM","BAVARIA","BAYLISS","BAYLOR","BEARING","BEATTIES","BEAUTY","BEAVERBROOKS","BEECHWOOD","BEEFEATER","BEELINE","BEFRIEND","BENBOW","BESTWAY","BEWS","BIKES","BLIND","BLINDCRAFT","BLINDS","BLOOD","BLOOMING","BLOSSOMS","BLOW","BLUEBELLS","BMT","BNA","BOARD","BOARS","BOAT","BODY","BOHEMIA","BOILERMAKERS","BON","BONADDIO","BONAVENTURE","BONIVIEW","BONNYMUIR","BOOKBINDING","BOOTS","BOROWSKI","BORSALINO","BOSANQUET","BOSCH","BOTTLED","BOULEVARD","BOURTREE","BOWEN","BOWLING","BOWTECH","BOX","BP","BPCC","BRA","BRACKENRIDGE","BRAEHEAD","BRAESIDE","BRAKE","BRIEF","BRITANNIA","BRITANNIC","BROAD","BROADCAST","BROADSTRAIK","BROS","BROTHERHOOD","BRUNSWICK","BUCKSBURN","BUDGET","BUILDING","BULAWEYO","BUREAU","BURNTHILLS","BUS","BUSINESS","CABER","CABINS","CABLE","CABS","CAFE","CAIRD","CAIRNCRY","CAIRNGORM","CAKE","CALEDON","CALEDONIA","CALEDONIAN","CALLANDERS","CALOR","CALTEC","CALVARYTABERNACLEUNITED","CAMARGUE","CAMEO","CAMPAIGN","CAMPBELLS","CAMPHILL","CAMWATER","CAN","CANALE","CANCER","CANDO","CANSCO","CAPE","CARADON","CARD","CAREERS","CARGO","CARPET","CARPETS"}&" "," "&b1&" "))),"YES","NO")'
You have a lot of quotes in there. Try rewriting it adding just one string at a time...once you get it working, then you can start adding more than one string at a time.

Resources