The test23 file contains a line
root_url = %(protocol)s://%(domain)s:%(http_port)s/
I need to replace this with
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
This below steps in the script doesn't work.
line_old='root_url = %(protocol)s://%(domain)s:%(http_port)s/'
line_new='root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana'
sed -i "s/\${line_old}\/\${line_new}/\g" test23
Try changing delimiter of sed:
sed -i "s|${line_old}|${line_new}|g" test23
example
echo "${line_old}" | sed "s|${line_old}|${line_new}|g"
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
I would use:
sed '/^root_url[ ][=]/s/$/grafana/' filename
Where:
/^root_url[ ][=]/ locates the line beginning with "root_url =", then
the normal substitution form 's/$/grafana/' simply appends "grafana" to the end of the line.
(note: add -i to edit the file in-place, or add -i.bak to edit in-place preserving a copy of the original file unchanged in filename.bak)
Results
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
Look things over and let me know if that meets your needs and if you have any questions.
Related
I need to replace few values and remove the "#" symbol for this lines in the below file using sed in my shell script.
Below are the lines to be replaced:
#RateLimitInterval = 30s
#RateLimitBurst = 1000
Replacement value:
RateLimitInterval = 60s
RateLimitBurst = 10000
File is as below
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitInterval=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
The code which I wrote:
sed -i.bk '/#RateLimit/ s/#//; /RateLimitInterval/ s/30s/60s/; /RateLimitBurst/ s/1000/10000/' check_jounal.conf
Can Someone provide me suggestions to optimize this code, feel like it is too long !!
I would just spell out the replacements in their entirety:
sed \
-e 's/^#RateLimitInterval=30s$/RateLimitInterval=60s/' \
-e 's/^#RateLimitBurst=1000$/RateLimitBurst=10000/'
By default, sed accepts its input on standard input and writes to standard output. You can add a filename to read the original from a file; add --in-place (-i) to also write the replacement to the same file.
sed -e '/RateLimitInterval/{s/30/60/; s/^#//;}' \
-e '/RateLimitBurst/{s/1000/10000/; s/^#//;}'
I strongly recommend against using -i. It seems a bit fragile to try to match the current RHS on each line, so you might prefer s/=.*/=60/' and s/=.*/=10000/'
I have a file "test.xml"
That looks like the below:
CLASS="BAKERS"
GROUP="ABCYYYYY"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX"
GROUP="ABCZZZZZ"
I want to use a single SED line command to find all occurrences of GROUP="ABC
Then within the "" I want add suffix: _DONE to all the matches found.
So the result should look like:
CLASS="BAKERS"
GROUP="ABCYYYYY_DONE"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX_DONE"
GROUP="ABCZZZZZ_DONE"
This is the command I am using:
`sed -i.bkp '/^GROUP="ABC/ s/$/_DONE"/' test.xml`
but it is appending after the " and not within the ""
It's almost that. But $ means end of line so you have to substitute the last " using "$ instead:
sed -i.bkp '/^GROUP="ABC/ s/"$/_DONE"/' test.xml
you could also specify that there is some blank after the " with for instance "[ \t]*$
this may help:
sed -i 's/^GROUP="ABC[^"]*/&_DONE/' file
Try doing this but without the backticks :
sed -i.bak '/^GROUP="ABC/s/"$/_DONE"/' file
You can use this sed command
sed '/GROUP="ABC/s/\(.*\)"/\1_DONE"/'
Output :
CLASS="BAKERS"
GROUP="ABCYYYYY_DONE"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX_DONE"
GROUP="ABCZZZZZ_DONE"
Use this:
sed -i.bkp 's/GROUP="ABC[A-Z]*/&_DONE/g' test.xml
I tested with your example and worked.
I have an input as follows
Input File Name : a.txt
-- Some Comment
delete from tb.Test WHERE id = 'abxd1';
delete from tb1.Test WHERE id = 'abxd2';
-- Some Comment
delete from tb1.Table1 WHERE id = 'abxd3';
Expected output file : b.txt
-- Some Comment
delete from Test WHERE id = 'abxd1';
delete from Test WHERE id = 'abxd2';
-- Some Comment
delete from Table1 WHERE id = 'abxd2';
The following code will just replace the value "tb.". I am trying to make this as a generic script.
while read line
do
str=$line
echo "${str/tb./}" >>b.txt
done <$1
Thanks for you help
You could use sed:
sed 's/delete from \(tb[0-9]\?\).\([[:alnum:]]\+\)/delete from \2/g' input.file
You can do it using sed:
echo $str | sed -r 's/tb[a-z0-9_]*.//g'
HTH
Assuming that you want to remove X. in delete from X.Y (everything before the DOT and the DOT), it is even simpler:
sed 's/delete from .*\./delete from /' a.txt
and you can use the -i option to overwrite the same file
sed -i 's/delete from .*\./delete from /' a.txt
HTH, Marcello
How can I replace a string in a file if line starts with another string using sed?
For example, replace this line:
connection = sqlite://keystone:[YOURPASSWORD]#[YOURIP]/keystone
With this line:
connection = mysql://keystone:password#10.1.1.10/keystone
Answer:
sed '/^start_string/s/search_string/replace_string/'
Information at http://www.gnu.org/software/sed/manual/sed.html#Addresses
If you want to change an entire line which starts with a pattern and ends with enything else, you can use command c description Example
sed -i '/^connection = sqlite/c\connection = mysql://keystone:password#10.1.1.10/keystone' your_file
You can do simply this :
sed -ri 's/sqlite/mysql/g' YOURFILE
sed '/^string1/ { s,string2,string3, }' file
This will replace string2 with string3 on all the lines that start with string1.
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