How to include special character in a jenkinsfile - jenkins

When i do a sed in my jenkinsfile as
"sh "sed -i 's|HEAP=.*|HEAP="\-Xms1024m \-Xmx1024m"|' $DIR/bin/myfile"
i get the error as unexpected char :'\'.
How to specify special characters in a jenkinsfile??

Try using
"""sh sed -i 's|HEAP=.*|HEAP="\-Xms1024m \-Xmx1024m"|' $DIR/bin/myfile"""

Use the \ character to escape a character.

Related

Jenkins sed Terraform shell

I have a Jenkins pipeline that I need to run a sed on a file but I am getting an error of line 2: syntax error: unexpected ")"
My file is this:-
name=""
age=""
My Jenkins sh line is:
"""sed -i -e 's|(name *= *")"|\1${params.NAME}"|g' -e 's|(age *= *")"|\1${params.AGE}"|g' vars.txt"""
I can run the sed on my shell fine and it works, but Jenkins doesn't like it for some reason.
If I run it through the Jenkins Pipeline Syntax Generator I get the same error.
You are using BRE POSIX pattern, and to create a capturing group there, you need to use escaped parentheses, \(...\). However, in the triple-quoted string literal, you need to escape the backslash to get a literal backslash in the resulting string.
You need to fix the line you have like this:
'''sed -i' ' -e 's|\\(name *= *"\\)"|\\1'"${params.NAME}"'"|g' -e 's|\\(age *= *"\\)"|\\1'"${params.AGE}"'"|g' vars.txt'''

Why this command does not work in Jenkins?

I am a Jenkins beginner.
Why does this command work?
sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g
And why does the same command not work when it is included in a Jenkinsfile?
sh "sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g"
The error is:
/opt/jenkins_data/workspace/secuview-front_master-Z2ADTSIGTSEJOG3UYRU4FPDUF5VZMB3SMQLEOUD46TUZG4POWKYQ#tmp/durable-a484faaf/script.sh: line 2: syntax error near unexpected token `('
Under the hood Jenkinsfiles are essentially Apache Groovy scripts, therefore string escaping rules for Groovy apply. When you have slashes they need to be escaped (e.g. \ -> \\) and when you're using double quotes using ${} literals actually get interpreted by the script instead of being passed to the shell step.
Try this instead:
sh 'sed -i -E s/\'image: \'\\(.*\\)${stack_name}-${service_name}:.*\\$/\'image: \'\\1${stack_name}-${service_name}:${version}/g'

jenkins escape sed command

Can someone escape this sed shell command in Jenkins groovy script for me?
So hard.
sh ("""
sed "s/(AssemblyInformationalVersion\(\")(.*)(\")/\1${productVersion}\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")
The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape". Since the first open paren is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash.
Thus:
sh ("""
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")
So if you like to replace some chars or word in an String groovy variable, for example replacing "/" with "/" in order to escape an special character, which in our case will be the forward slash you can use the code below.
So afterwards we'll be able to apply the linux sed command without getting an error (for instance using sed to replace a place holder value with the desired value in an .env file).
Below we show a Jenkins Pipeline Groovy code:
String s = "A/B/C"
your_variable = s.replace("/", "\\/")
sh "sed -i -e 's/string_to_replace/${your_variable}/g' path/to/file/.env"
NOTE: ${your_variable} will get the content of your variable.
VALIDATION:
echo "Your variable new content: ${your_variable}"
RESULT:
Your variable new content: A\/B\/C

replace ruby code in all files with unix command

I have a string !Rails.env.dev? || params['render_javascript'].
I want to replace this string with render_javascript. It will be lots of work if I do it one file by one file.
I tried to use unix command as follows, but no luck.
for i in $(find . -name "*.rb")
do
       sed 's/!Rails\.env\.dev\? \|\| params\['render_javascript'\]/render_javascript/g' $i > x
       mv x $i
done
Anyone can offer me some help here?
There are few issues in your code/command. First, you are escaping characters unnecessarily. Second, if we use double quotes to quote sed command it will be better in this case. You can try below command:
for i in $(find . -name "*.rb")
do
sed "s/\!Rails\.env\.dev? || params\['render_javascript'\]/render_javascript/g" $i > x
mv x $i
done
sed (GNU sed) uses Basic Regular expression (BRE) which does not have ?, | metacharacters, so you do not have to escape them. Metacharacters ?, | are added by Extended Regular Expresson (ERE).
And, I have used double quotes " to quote the sed command, because there are single quotes ' in your search regex. And because double quotes was used to quote the sed command, we also have to escape the ! character. We have to escape ! because it is a shell special character and expands to something else, even before sed command is run.
If you want to use Extended regular expression (ERE), then you can use -r option with the sed command. In this case, you have escape ? and | character. For example:
sed -r "s/\!Rails\.env\.dev\? \|\| params\['render_javascript'\]/render_javascript/g" $i > x

Inserting a matched string from previous line to the current line using sed or awk

I have a CSV file that shows the statistics for links on a half an hour basis. The link name only appears on the 00:00 line.
link1,0:00,0,0,0,0
,00:30,0,0,0,0
,01:00,0,0,0,0
,01:30,0,0,0,0
,02:00,0,0,0,0
,02:30,0,0,0,0
,03:00,0,0,0,0
,03:30,0,0,0,0
,23:30,0,0,0,0
....
....
link2,00:00,0,0,0,0
How do I copy the link name to every other line until the link name is different, using sed or awk?
With awk, just keep track of the last seen non-empty link name, and always use that.
awk -F, -v OFS=, '$1 != "" { link=$1 } { $1 = link; print $0 }'
Omitting the ellipses, this gives:
link1,0:00,0,0,0,0
link1,00:30,0,0,0,0
link1,01:00,0,0,0,0
link1,01:30,0,0,0,0
link1,02:00,0,0,0,0
link1,02:30,0,0,0,0
link1,03:00,0,0,0,0
link1,03:30,0,0,0,0
link1,23:30,0,0,0,0
link2,00:00,0,0,0,0
This is a simpler job with awk, but if you want to use sed:
sed -e '/^[^,]/{h;s/,.*//;x};/^,/{G;s/^\(.*\)\n\(.*\)/\2\1/}'
Bellow a commented version in sed script file format that can be run with sed -f script:
# For lines not beginning with a ',', saves what precedes a ',' in the hold space and print the original line.
/^[^,]/{
h
s/,.*//
x}
# For lines beginning with a ',', put what has been save in the hold space at the beginning of the pattern space and print.
/^,/{
G
s/^\(.*\)\n\(.*\)/\2\1/}
You can do that in pure bash shell without needing to start a new process, which should be faster than using awk or sed:
IFS=","
while read v1 v2; do
if [[ $v1 != "" ]]; then
link=$v1;
fi
printf "%s,%s\n" "$link" "$v2"
done < file

Resources