Use sed in groovy to replace a value in json file - groovy

I have a json file with the below content:
"containerDefinitions": [
{
"image": "***.dkr.ecr.us-east-1.amazonaws.com/xyz"
}
]
Now i want to replace the url for image with a new value. So in my jenkins scripted groovy file, i store this existing url value under some variable and then enter the new url value appended by build number. So i try to do the following:
newimageurl="\"***.dkr.ecr.us-east-1.amazonaws.com/xyz:v_$BUILD_NUMBER\""
oldimageurl="\"***.dkr.ecr.us-east-1.amazonaws.com/xyz\""
sed -i -e 's#'"$oldimageurl"'#'"$newimageurl"'#' ./myfile.json
But it ends with error both for syntax for newimageurl for the v_$BUILD_NUMBER and then for sed command.
How to resolve this?

When defining the string variable in groovy, you don't need to add the double quotes. Also, you have an error in the interpolation. You'll do:
newimageurl = "***.dkr.ecr.us-east-1.amazonaws.com/xyz:v_${BUILD_NUMBER}"
oldimageurl = "***.dkr.ecr.us-east-1.amazonaws.com/xyz"
In you sed command as well (also, you have to put it inside an sh command):
sh """
sed -i -e 's#${oldimageurl}#${newimageurl}#' ./myfile.json
"""

Related

How to replace string in a array using sed in shell script

I have a array declared in a file and i want to replace/update array with latest values.
below is the array saved in a file.
declare -A IMAGES_OVERRIDE
IMAGES_OVERRIDE=(
[service1]='gcr.io/test-project/image1:latest'
[service2]='gcr.io/test-project/image2:9.5.16'
[service3]='gcr.io/test-project/data/image3:latest'
)
Now I want to update service2 with latest image gcr.io/test-project/image2:10.0.1 and save into file.
I tried like below
sed -i 's/[service2]=.*/[service2]='gcr.io/test-project/image2:10.0.1'/' ./override
but I am getting below error.
sed: -e expression #1, char 35: unknown option to `s'
Same command is working me for other script but that is not array.
Simply:
> sed -i "s#\[service2\]=.*#[service2]='gcr.io/test-project/image2:10.0.1'#" ./override
Notes:
Use " instead of ' around sed expression (your sed script contains ');
Use # instead of / to limit each part of sed replace expression (your new token contains /);
Use \ before each [ and ] in RE expression ([ and ] are special RE characters);

sed command with dynamic variable and spaces - not retaining spaces

I am trying to insert a variable value into file from Jenkinsfile using shell script in the section
Variable value is dynamic. I am using sed.
Sed is working fine but it is not retaining the white spaces that the variable have at the beginning.
ex:
The value of >> repoName is " somename"
stage('trying sed command') {
steps {
script {
sh """
#!/bin/bash -xel
repo='${repoName}'
echo "\$repo"
`sed -i "5i \$repo" filename`
cat ecr.tf
"""
}
}
}
current output:
names [
"xyz",
"ABC",
somename
"text"
]
Expected output:
names [
"xyz",
"ABC",
somename
"text"
]
How do i retain the spaces infront of the variable passing from sed
With
$ cat filename
names [
"xyz",
"ABC",
"text"
]
$ repo=somename
we can do:
sed -E "3s/^([[:blank:]]*).*/&\\n\\1${repo},/" filename
names [
"xyz",
"ABC",
somename,
"text"
]
That uses capturing parentheses to grab the indentation from the previous line.
if $repo might contain a value with slashes, you can tell the shell to escape them with this (eye-opening) expansion
repo='some/name'
sed -E "3s/^([[:blank:]]*).*/&\\n\\1${repo//\//\\\/},/" filename
names [
"xyz",
"ABC",
some/name,
"text"
]
I used 1 sed statement to add the content first to the file and then another sed statement for just adding spaces. This fixed my issue. All day i was trying to fit in one command did not work probably from Jenkins and shell usage. But using 2 sed commands as a workaround i was able to finish my task
The i command skips over spaces after the i command to find the text to insert. You can put the text on a new line, with a backslash before the newline, to have the initial whitespace preserved.
stage('trying sed command') {
steps {
script {
sh """
#!/bin/bash -xel
repo='${repoName}'
echo "\$repo"
`sed -i "5i \\\\\\
\$repo" filename`
cat ecr.tf
"""
}
}
}
I've tested this from a regular shell command line, I hope it will also work in the Jenkins recipe.

How to read a file and replace a string in another file using shell script in linux?

I am try to read the value of the key from the text file. After reading it I wanted to concatenate this value with some string and replace that line in another file. Please find the below code
source.txt file
USERNAME='cffde308-a1f8-436f-9750-619856e70b88'
PASSWORD='XEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0='
target.js file
auth: {
username: 'zfgde308-a1f8-436f-9750-619856e70b89',
password: 'ZEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0=',
},
I wanted to copy username and password from the source file and replace it in the config file. I tried the below code
#! /bin/sh
source "/opt/source/source.txt"
USER="username: '$USERNAME',"
PWD="password: '$PASSWORD',"
echo $USER
echo $PWD
sed '/username/c\$USER' target.js
sed '/password/c\$PWD' target.js
And the result that i get it is
[l21m23 source]$ ./ClientDetailsSetup.sh
',ername: 'cffde308-a1f8-436f-9750-619856e70b88
password: 'XEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0=',
auth: {
username: 'zfgde308-a1f8-436f-9750-619856e70b89',
$PWD
},
If some help at the earliest to resolve this issue would be of great help
#!/bin/sh
source "/opt/source/source.txt"
USERN="username: '$USERNAME',"
PWD="password: '$PASSWORD',"
echo $USERN
echo $PWD
sed -i "s/username:.*/$USERN/" target.js
sed -i "s/password:.*/$PWD/" target.js
There were a few issues:
USER is a reserved shell variable so you have to give it a different name
your sed script needs s/.../.../ to substitute and you also need to match the rest of the line in your regexp so the .* is needed or the old credentials are still in the output
-i option on sed is needed for an in-place edit
variable substitution is not performed in a string delimited with ', so changed to " here

Find and return an if code block from a file

I am writing a bash script to find if a code block starting with
if (isset($conf['memcache_servers'])) { exists in a file?
If true, then I need to return the whole if block.
How to do that?
Code block return example:
if (isset($conf['memcache_servers'])) {
$conf['cache_backends'][] = '.memcache.inc';
$conf['cache_default_class'] = 'MemCache';
$conf['cache_class_cache_form'] = 'DatabaseCache';
}
You can use sed to do this. From a bash command line, run this:
sed -n "/if (isset(\$conf\['memcache_servers'\]))/,/}/p" inputFile
This uses range option /pattern1/,/pattern2/ from sed, and p to print everything between and including the if...{ and } lines.
Here, I have used double quotes to express the sed script because the first pattern includes single quotes'. he sqaure-brackets need to be escaped as well. \[ and \].

bash string manipulation - Display the value, not the variable name

I'm writing a script to process inbound data files. The inbound file names all follow the same pattern:
word1_word2_word3_YYYYMMDD.txt
My script takes the name of the inbound file, strips the file extension, strips out the date, replaces all underscores with spaces and appends the resulting string to each line in the original file. I can succesfully create the desired string and have assigned it to a variable "STR"
The last step is to append the value of $STR to each line in the file so that the data lines within the file end up looking like this:
casenumber1"|"word1 word2 word3
casenumber2"|"word1 word2 word3
casenumber3"|"word1 word2 word3
My problem is that for the life of me I cannot get bash to display the variable value, it always displays the variable name.
This is the line I use to create the string needed from the file name:
STR=`echo $DATAFILENAME | cut -d '.' -f 1 | sed 's/[0-9]*//g'|sed 's/_/ /g' | sed 's/[[:blank:]]*$//'`
I'm trying to use a typical sed replace command:
sed 's/$/`echo "$STR"`/g' inputfile > outputfile
But keep getting the variable name instead of the variable value:
example output:
1000056|$"STR"
1000057|$"STR"
...
desired output:
1000056|Closed With Notification
1000057|Closed With Notification
What am I doing wrong? Thanks, Vic
The gist of your question is that you need to add a string to a file using sed and the value of that string is contained in a variable, which you call "a", as we read in the final list.
Then you need use this combination, which is missing from your list above:
sed "s/$/| $a/g" $DATAFILE > datfile99
The problem is that the single quotes around your command prevent the interpolation of the variable $a.
If you wrap the command in double quotes the whole string will be passed to sed after that the shell replaces $a to its current value.
Try replacing your ' with " this will tell your shell to substitute any shell variables
sed -i "s/$/echo $STR/g"
Note -i option will make actual changes to your file, hence it is wise to backup.
EDIT: instead of using this
STR=`echo $DATAFILENAME | cut -d '.' -f 1 | sed 's/[0-9]*//g'|sed 's/_/ /g' | sed 's/[[:blank:]]*$//'`
You can try this
sed -i -r "s/(.*)[.][a-zA-Z]+$/\\1/g;s/[._]/ /g" <<< "$DATAFILENAME"

Resources