Remove String Captured in Regex Group - sed

I have a file that contains the following content with tags/tag/volume_tags. I want to use sed command to remove stackId = AnyValue inside tags/tag only (not volume_tags).
Input:
tags = {
Name = var.stack_id
StackId = bar
}
tags =
{
Name = var.stack_id
StackId = bar
}
tag = {
StackId = foo
}
volume_tags = {
StackId = bar
}
Output:
tags = {
Name = var.stack_id
}
tags =
{
Name = var.stack_id
}
tag = {
}
volume_tags = {
StackId = bar
}
I try creating Regex that captures the tags/tag block and group StackId = AnyValue with (\s*StackId.\s*=.*) and aim to use sed command to replace that group with an empty string. How to do so using sed? or any other suggestion to achieve this?
^tag.*\s*=?\s*{(\s*.*\s*)?(\s*StackId.\s*=.*)\s*}

This might work for you (GNU sed):
sed '/^tags\?\>/{:a;n;/\<StackId\>/d;/^}$/!ba}' file
If a line begins with tag or tags fetch the following lines until one beginning/ending }.
If the collection of lines above contains the word StackId delete that line only.
N.B. Assumes only one such id per stanza.
cat <<\! | sed -f - file
/^tags\?\>/{
:a
n
/\<StackId\>/d
/^}$/!ba
}
!

sed is line-oriented. It can perform actions on ranges of lines:
sed -e '
/^[[:space:]]*tags\{0,1\}[[:space:]]*=[[:space:]]*{[[:space:]]*$/, /^[[:space:]]*}[[:space:]]*$/ {
/^[[:space:]]*StackId[[:space:]]*=/ d
}
'
\{0,1\} is equivalent to more usual ? in other regex versions
sed can't portably do case-insensitive matching but there are workarounds

Related

PowerShell run exe file and pass args as string var

I'm creating a power shall scrip to run exe file with arguments. The list of args are constructed in a way that if value of the arg is empty or null, the parameter shall not be passed
Below is my script
$runnerCommand = " "
[string]$splitRun = "20:1"
[string]$maxTestWorkers = "777"
[string]$retryTimes = "9"
[string]$testFilterInXmlFormat = "<filter><cat>XX</cat></filter>"
#$runnerCommand += '--testDllPath ' + $testDllPath + " "
if ($splitRun){
$runnerCommand+= "--splitRun '$splitRun' "
}
if ($maxTestWorkers){
$runnerCommand+= "--maxTestWorkers '$maxTestWorkers' "
}
if ($retryTimes){
$runnerCommand+= "--retryTimes '$retryTimes' "
}
if ($testFilterInXmlFormat){
$runnerCommand+= "--testFilterInXmlFormat '$testFilterInXmlFormat' "
}
$cmdPath = "C:\AutoTests\TestAutomation.Runner\bin\Debug\TestAutomation.Runner.exe"
& $cmdPath --testDllPath C:/AutoTests/Build/TestAutomation.TestsGUI.dll $runnerCommand
It looks like that PowerShell do a 'new line' before $runnerCommand in the last line of code that results in not passing the args from $runnerCommand
Please suggest how to solve the problem.
I tried different approaches
You're currently passing all of the arguments as a single string. You should instead use an array with each argument as a separate element. I can't actually test this, but something like this should work:
[string]$splitRun = "20:1"
[string]$maxTestWorkers = "777"
[string]$retryTimes = "9"
[string]$testFilterInXmlFormat = "<filter><cat>XX</cat></filter>"
$runnerArgs = #(
'--testDllPath', 'C:/AutoTests/Build/TestAutomation.TestsGUI.dll'
if ($splitRun) {
'--splitRun', $splitRun
}
if ($maxTestWorkers) {
'--maxTestWorkers', $maxTestWorkers
}
if ($retryTimes) {
'--retryTimes', $retryTimes
}
if ($testFilterInXmlFormat) {
'--testFilterInXmlFormat', $testFilterInXmlFormat
}
)
$cmdPath = "C:\AutoTests\TestAutomation.Runner\bin\Debug\TestAutomation.Runner.exe"
& $cmdPath $runnerArgs
Note that PowerShell allows expressions, including if-expressions, inside the array sub-expression operator (#()).

Typoscript modify a field:variable

I have this typoscript:
tt_content.gridelements_pi1.20.10.setup {
3cols.outerWrap = <div>|</div>
3cols.outerWrap.override.insertData = 1
3cols.outerWrap.override = <div id="{field:tx_cewrap_id_input}" class="{field:tx_cewrap_class_input} {field:tx_cewrap_class_select}">|</div>
3cols.outerWrap.override.if.isTrue.field = tx_cewrap_active
}
Which makes sure a wrapper is made around a certain element. The following html is generated as an example:
<div id="" class="full-box full-box-features container pt-75,pb-75"></div>
As you can see there is a comma separated string inserted as "tx_cewrap_class_select". With the {field:tx_cewrap_class_select} part:
pt-75,pb-75
But i want the comma to be a space character so the classes work in html
Now I know about the split option
But how do I fix the code, just need somehow to remove the comma! That's it :)
Thanks in advance for any response I can use.
You can split by comma and join with a space, but in this case it might be easier just to replace the comma by space:
10 = TEXT
10.replacement {
1 {
search = ,
replace.char = 32
}
}
And here the solution with split. it should be obvious why not to use:
10 = TEXT
10.split {
token = ,
cObjNum = 1 || 2
1.current = 1
2.current = 1
2.noTrimWrap = | ||
}
Hint:
on TEXT you can use stdWrap functions immediately,
in other context you might need an explicit .stdWrap:
10.stdWrap.replacement {
10.stdWrap.split {
Either you prepare your values into a register for later use, or you split your override value into a COA. You even might use the replacement on the whole override value in case you are sure no other comma might be needed.
COA-Solution:
(don't forget the noTrimWrap for 20 otherwise the classes are appended without space)
override.cObject = COA
override.cObject {
10 = TEXT
10.value <div id="{field:tx_cewrap_id_input}" class="{field:tx_cewrap_class_input}
10.insertData = 1
20 = TEXT
20.field = tx_cewrap_class_select
20.replacement {
1 {
search = ,
replace.char = 32
}
noTrimWrap= | ||
}
30 = TEXT
30.value = ">|</div>
}

TYPO3 : editing page title

I try to get page title and edit it like on the following example.
Page name My tools
Final string my_tools (Thus, I will be able to use it through markers in my css classes)
I know how to get page title using:
HEADERTITLE = TEXT
HEADERTITLE.data = page : title
But how can I transform this string?
Thank you for your help!
to convert the case and replace some characters you may use these TypoScript settings:
HEADERTITLE = TEXT
HEADERTITLE {
data = page:title
### replace whitespace
replacement.10 {
search = #\s#i
replace = _
useRegExp = 1
}
/*
### replace all special characters
replacement.10 {
search = #\W#i
replace =
useRegExp = 1
}
*/
### transform string to lowercase
case = lower
}

Removing comments with JFlex, but keeping line terminators

I'm writing lexical specification for JFlex (it's like flex, but for Java). I have problem with TraditionalComment (/* */) and DocumentationComment (/** */). So far I have this, taken from JFlex User's Manual:
LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]
WhiteSpace = {LineTerminator} | [ \t\f]
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} | {DocumentationComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}
DocumentationComment = "/**" {CommentContent} "*"+ "/"
CommentContent = ( [^*] | \*+ [^/*] )*
{Comment} { /* Ignore comments */ }
{LineTerminator} { return LexerToken.PASS; }
LexerToken.PASS means that later I'm passing line terminators on output. Now, what I want to do is:
Ignore everything which is inside the comment, except new line terminators.
For example, consider such input:
/* Some
* quite long comment. */
In fact it is /* Some\n * quite long comment. */\n. With current lexer it will be converted to a single line. The output will be single '\n'. But I would like to have 2 lines, '\n\n'. In general, I would like that my output will always have the same number of lines as input. How to do it?
After couple of days I found a solution. I will post it here, maybe somebody will have the same problem.
The trick is, after recognizing that you are inside a comment - go once more through its body and if you spot new line terminators - pass them, not ignore:
%{
public StringBuilder newLines;
%}
// ...
{Comment} {
char[] ch;
ch = yytext().toCharArray();
newLines = new StringBuilder();
for (char c : ch)
{
if (c == '\n')
{
newLines.append(c);
}
}
return LexerToken.NEW_LINES;
}

How to split data in a text file in C#

.This is how my data looks: name: abcsurname: abctel:1234 and I want it to look like:name: abc and surname: abc should go in the next line.
public void SeparateData()
{
//read file
StreamReader sr = new StreamReader("myTextFile.txt");
//string to hold line
string myline;
myline = sr.ReadLine();
while ((myline = sr.ReadLine()) != null)
{
string[] lines = Regex.Split(myline, " ");
foreach (string s in lines)
{
using (StreamWriter sw = new StreamWriter("myTextFile.txt"))
sw.WriteLine(lines);
}
}
}
Well, you don't need regex to do a string split on a space, but I don't that is going to get you want you want either way. I am assuming that "abc" stands in for actual values of varying length?
I think you just need to pull out your regex book and split up the string and rewrite it.
e.g. '(name: )(\w*?)(surname: )(\w*?)(tel: )(\d*)' and then just re-assemble and re-write the line using the capturing groups.
I think what you have will give you:
name:
abcsurname:
abctel:
1234