I am trying to add this block to after my first line in a file:
sed -i '1a\
apply plugin: 'io.fabric' \
repositories { \
maven { \
url 'https://maven.fabric.io/public' \
} \
}' test.txt;
But on running interminal I get error:
sed: -e expression #1, char 108: unexpected `}'
Two things,
You are including single quotes within single quotes, it will have undesirable results. Use double quotes or properly quote single quotes within the string.
Nothing should follow the \ at the end of the lines.
We could write something like,
sed "1a \\
apply plugin: 'io.fabric' \\
repositories { \\
maven { \\
url 'https://maven.fabric.io/public' \\
} \\
}" input
Note Here \\ is required as we are using double quotes.
Related
I have this macro:
`define do_code(DO_SOETHING, ID) \
fork \
begin \
``DO_SOMETHING`` \
end \
begin \
$display("%s",ID.name()); \
end \
join_any \
disable fork; \
and I use it as such:
`do_code($display("%s",argA.name()), argB)
How does the compiler knows to separate the two macro's input arguments correct:
DO_SOMETHING = $display("%s",argA.name())
ID = argB
Why not break it to:
DO_SOMETHING = $display("%s"
ID = argA.name()), argB
???
The compiler knows because the IEEE 1800-2017 SystemVerilog LRM
says in section 22.5.1 `define
Actual arguments and defaults shall not contain comma or
right parenthesis characters outside matched pairs of left and right parentheses (), square brackets [],
braces {}, double quotes "", or an escaped identifier.
The comma in your fist argument is inside a matched pair of parenthesis.
BTW, you should not be using `` unless you are trying to create a new identifier by joining macro argument with text in the body of the macro,
val str= " This string has " , need to escape with \ .Even string has \ before"
val resultShouldbe=" This string has \" ,need to escape with \\.Even string has \\ before"
str.replace(""""""" , """\"""").replace("\\","\\\\")
The output of first replace is adding up to the second replace.
Kindly help.
str.replaceAll("([\"\\\\])" , "\\\\$1")
Matching regex:
(...) - capture group: Capture everything that matches this pattern.
[...] - character class: Match any of the given characters.
\"\\\\ - 2 characters: A quote mark (escaped) or a backslash (doubly escaped).
Replacement string:
\\\\$1 - 2 elements: A backslash (doubly escaped) followed by whatever was captured in the 1st capture group. (In this case there was only 1 capture group.)
In other words: For every quote " or backslash \ character, replace it with the same character preceded by a backslash \ character.
I want to run the following PowerShell script file from Jenkins Pipeline:
".\Folder With Spaces\script.ps1"
I have been able to do it with the following step definition:
powershell(script: '.\\Folder` With` Spaces\\script.ps1')
So I have to remember to:
escape the backslash with a double backslash (Groovy syntax)
escape the space with backtick (PowerShell syntax)
I would prefer to avoid at least some of this. Is it possible to avoid using the backtick escaping, for example? (Putting it between "" does not seem to work, for some reason.)
I found that it's possible to use the ampersand, or invoke, operator, like this:
powershell(script: "& '.\\Folder With Spaces\\script.ps1'")
That gets rid of the backtick escaping, and should make life a tiny bit easier.
To avoid escaping the backslashes you could use slashy strings or dollar slashy strings as follows. However you cannot use a backslash as the very last character in slashy strings as it would escape the /. Of course slashes as well would have to be escaped when using slashy strings.
String slashy = /String with \ /
echo slashy
assert slashy == 'String with \\ '
// won't work
// String slashy = /String with \/
String dollarSlashy = $/String with / and \/$
echo dollarSlashy
assert dollarSlashy == 'String with / and \\'
And of course you'll lose the possibility to include newlines \n and other special characters in the string using the \. However as both slashy and dollar slashy strings have multi line support at least newlines can be included like:
String slashyWithNewline = /String with \/ and \
with newline/
echo slashyWithNewline
assert slashyWithNewline == 'String with / and \\ \nwith newline'
String dollarSlashyWithNewline = $/String with / and \
with newline/$
echo dollarSlashyWithNewline
assert dollarSlashyWithNewline == 'String with / and \\ \nwith newline'
If you combine that with your very own answer you won't need both of the escaping.
I'm trying to use sed to replace a specific line within a configuration file:
The pattern for the line I want to replace is:
ALLOWED_HOSTS.*
The text I want to insert is:
'$PublicIP' (Including the single ticks)
But when I run the command:
sed 's/ALLOWED_HOSTS.*/ALLOWED_HOSTS = ['$PublicIP']/g' /root/project/django/mysite/mysite/settings.py
The line is changed to:
ALLOWED_HOSTS = [1.1.1.1]
instead of:
ALLOWED_HOSTS = ['1.1.1.1']
How shall I edit the command to include the single ticks as well?
You could try to escape the single ticks , or better you can reassign the variable including the simple ticks:
PublicIP="'$PublicIP'".
By the way even this sed without redifining var, works ok in my case:
$ a="3.3.3.3"
$ echo "ALLOWED_HOSTS = [2.2.2.2]" |sed 's/2.2.2.2/'"'$a'"'/g'
ALLOWED_HOSTS = ['3.3.3.3']
Even this works ok:
$ echo "ALLOWED_HOSTS = [2.2.2.2]" |sed "s/2.2.2.2/'$a'/g"
ALLOWED_HOSTS = ['3.3.3.3']
Hi I want to replace a string coming between to symbols by using sed
example: -amystring -bxyz
what to replace mystring with ****
value after -a can be anything like -amystring 123 -bxyz, -amystring 123<newline_char>, -a'mystring 123' -bxyz, -a'mystring 123'<newline_char>
I tried following regex but it does not work in all the cases
sed -re "s#(-w)([^\s\-]+)#\1**** #g"
can anybody help me to solve this issue ?
MyString="YourStringWithoutRegExSpecialCharNotEscaped"
sed "s/-a${MyString} -b/-a**** -b/g"
if you can escape your string for any regex key char like * + . \ / with something like
echo "${MyString}" | sed 's/\[*.\\/+?]/\\&/g' | read -r MyString
before us it in sed.
otherwise, you need to better define the edge pattern