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);
Related
I have command that gives me following output:
'First' : 'abc',
'Second' :'xyz',
'Third' :'lmn'
Requirement here is to convert this output into valid json format.
So I replaced all ' to " using sed :
<command> | sed "s/'/\"/g"
"First" : "abc",
"Second" :"xyz",
"Third" :"lmn"
Now I also need to add { in the begining and end of the output how can I do that.
Any other thoughts are also welcome.
sed -z "s/[[:space:]]*'\([^']*\)'[[:space:]]*:[[:space:]]*'\([^']*\)'[[:space:]]*/"'"\1":"\2",/g; s/,$//; s/^/{/; s/$/}/'
First match the '<this>' : '<and this>'
Then convert each such sequences into "<this>":"<and this>",
Remove trailing comma.
Add { } in front of it.
-z is a GNU extension to parse it all as one line. Alternatively you could remove newlines before passing to sed.
|sed -e '1s/^/{/' -e "s/'/"/g" -e '$s/$/}/' does the work.
execute PKG_SP_MAINTENANCE.MoveAccount(91, 129031, 958408630); Lowes
From the above statement I am trying to get the content between the first comma and second comma i.e., 129031 and replace it with a new string which is passed as a parameter to the script. For now let's replace with N . I tried the following sed command ended up getting an error. Could someone please help?
04:24:01 Tue Sep 19 [serviceb#LQASRDSBMST002V:~/isops/tmp] cat Navya | sed 's/,^.\{*\},/N/'
sed: -e expression #1, char 14: Invalid content of \{\}
$: echo "start,middle,end" | sed 's/,[^,]*,/,NEW,/g'
start,NEW,end
Is this what you mean? This simply matches the inner-most commas and replaces the text.
Depending how you want to handle strings with more than two commas, you could do something like this to match the outer-most instead:
$: echo "start,middle,end" | sed 's/,.*,/,NEW,/g'
start,NEW,end
this is my shell script to replace the string
OPTIONS="-p ${PIDFILE}"
with multi line string values
OPTIONS_1234="-p ${PIDFILE_1234} -ORBEndpoint iiop://localhost:1234"
OPTIONS_1235="-p ${PIDFILE_1235} -ORBEndpoint iiop://localhost:1235"
OPTIONS_1236="-p ${PIDFILE_1236} -ORBEndpoint iiop://localhost:1236"
based on my input port number , that many OPTIONS should be created.
#!/bin/bash
NS_HOSTNAME= localhost
namingService_ports = 1234,1235,1236
IFS=','
read -r -a portArray <<< "$namingService_ports"
for port in ${portArray[#]}; do
sed '0,/\PTIONS="-p ${PIDFILE}"/ s//\OPTIONS_'"$port"'="-p ${PIDFILE_'"$port"'} -ORBEndpoint iiop:\/\/'"$NS_HOSTNAME"':'"$port"'"\n /' "/etc/init.d/tao" > "tao_ns1"
done
can some suggest me how sed command will take the multi line with for loop and move to the file "tao_ns1"
You basically have 3 issues with your script.
The assignment namingService_ports = 1234,1235,1236 is incorrect in bash and it should be without spaces, i.e. namingService_ports="1234,1235,1236"
There is a typographical error in first sed option for OPTIONS i.e. sed '0,/\PTIONS="-p ${PIDFILE}" should have been sed '0,/\OPTIONS="-p ${PIDFILE}"
And while writing to a file "tao_ns1" you are using the > operator which basically over-writes the file every-time you run it. It should have been the >> append operator, which appends the new line to the file for every iteration of the loop.
Also http://www.shellcheck.net/, that you have carriage return characters in your script, run tr command on it before proceeding next.
tr -d '\r' < current_script.sh > new_script.sh
With the above fixes made.
#!/bin/bash
NS_HOSTNAME="localhost"
namingService_ports="1234,1235,1236"
IFS=','
read -r -a portArray <<< "$namingService_ports"
for port in ${portArray[#]}; do
sed '0,/\OPTIONS="-p ${PIDFILE}"/ s//\OPTIONS_'"$port"'="-p ${PIDFILE_'"$port"'} -ORBEndpoint iiop:\/\/'"$NS_HOSTNAME"':'"$port"'"\n /' "/etc/init.d/tao" >> "tao_ns1"
done
should work fine.
Two examples that I hope helps to reach want you want to ...
Changing a string and redirect the result to a new file, replacing AAA to BBB:
echo "xxxAAAxxx" | sed "s/AAA/BBB/i" > out.ini
Same logic (replacing BBBto CCC), but at this time replace some string inside an existing file
sed -i "s/BBB/CCC/i" out.ini
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"
My Stress.k file is as follows
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*END
I want it to be like
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*/home/hassan/534.k
*END
for that I used sed as follows
a="$(cat flow.k)"
sed -i -e '/*END/i \*/home/hassan/$a.k ' Stress.k
where flow.k has only a single number like 534.k or something . Here sed put the line before END but it doesn't take the value of a , instead it puts the same alphabet and it doesn't understand $a.k.
Please also tell me how to delete the second last line or the line with a string hassan for example so that I can delete it first and the for the next step I use it to enter my required line.
if possible please also suggest the alternatives.
best regards
bash variables are only replaced when in double quotes, e.g.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
Use double quotes to allow the variable to be expanded.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
To replace the string, do it as you read in the file:
a=$(sed 's/534/100/' flow.k)
To delete a line:
sed '/hassan/d' inputfile
To read a file into the stream after the current line:
sed '/foo/r filename' inputfile