Dynamically replace string value in configuration file - shell

I'm building a shell script to install apache-airflow and update some configurations for local development.
I would like to replace the value of property dags_folder. How can I replace it using shell script?
Here is what I've tried:
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_EXTRA=" "
else
SED_EXTRA=""
fi
sed -i${SED_EXTRA}'' "s,dags_folder = ,dags_folder = banana,g" ~/airflow/airflow.cfg
It's almost working but I want to replace entire value. Is possible?

Don't use sed at all. Use ed, which won't require OS-specific changes to the invocation.
You want to match the entire line, not just the name = prefix, so add .* to the regular expression.
ed ~/airflow/airflow.cfg <<EOF
s,dags_folder = .*,dags_folder = banana,g
wq
EOF

Match up until the end of the line with .* regex
sed -i${SED_EXTRA}'' 's,dags_folder = .*,dags_folder = banana,g' ~/airflow/airflow.cfg

try this one if I understand your issue correctly :)
var1="dags_folder ="
var2="dags_folder = banana"
sed -i 's/'"$var1"'/'"$var2"'/g' path to your cfg file

Related

pass dynamic value in the sed command in unix inside IT block ruby

output.txt:
select * from table startTime.... (very big query)
abc.rb:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/$time/g' ./db/output.txt`
end
expected output----
output.txt:
select * from table 6537290102....
In output.txt, the value should be 6537290102 please someone help. thanks in advance
If I hardcode the time value as shown below in sed command then it is working fine whereas the passing the dynamic value "time" is not working
replacestarttime = sed -i -e 's/startTime/6537290102/g' ./db/output.txt
Replace single quotes with double quotes
`sed -i -e "s/startTime/$time/g" ./db/output.txt`
Interpolate Your String in Ruby
Right now, you're expecting sed to access a shell variable named $time. This variable doesn't exist, and wouldn't work as-is even if it did.
To fix this, you should to interpolate your Ruby variable into the string passed to sed. You can do this as follows:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/#{time}/g' ./db/output.txt`
end

Trying to comment a source file

I am trying to comment lines of source so that something like
LANG = 'ENG';
becomes
// LANG = 'ENG';
There are over a thousand lines in the source file and 'ENG' is not unique but the entire line IS.
I gave up on wild carding the spaces and just tried the entire extant line 'as-is' but no joy.
Something like (commented shell)--
enter code here
#!/bin/bash
#if [ -n "$5" ] ; then
#if [ "$5" == "ENG" ] ; then
sed -i "s/' LANG = '\''ENG'\''/\/\/' LANG'
= '\''ENG'\''/" vc.pas > vc.out
#fi
#fi
So it reduces down to a single line. No joy whatever I try.
TIA !
Howie
This works for me, passing any whitespace through to the output:
$ echo "LANG='ENG';" | sed "s#^\(\s*LANG\s*=\s*'ENG'\s*;\)#// \1#"
// LANG='ENG';
$ echo " LANG = 'ENG' ; " | sed "s#^\(\s*LANG\s*=\s*'ENG'\s*;\)#// \1#"
// LANG = 'ENG' ;
Technically it needs double backslashes inside the double-quoted string, but because none of the sequences form a valid escape sequence, bash doesn't mind.
With GNU sed, use
sed -i "s,.*LANG *= *'ENG';.*,//&," vc.pas
where
-i - enables inline file modification
s - substitution command
, - delimiter
.*LANG *= *'ENG';.* - text containing LANG = 'ENG'; with any amount of spaces around =
//& - replaces the matched line with // and the line itself
Use a regular expression for the line selection, followed by a substitution. Put the replacement commands in a file:
$ cat dt.sed
/^LANG[[:space:]]*=[[:space:]]*'[^']*'/s;^;// ;
$
Then run sed(1) with that script:
$ echo "LANG='FOO'" | sed -f dt.sed
// LANG='FOO'
This works on a Fedora 34 system, but should work on any Linux.

Replace strings with variable in bash script

I want to create a bash script to change the configuration file of an application. The script will read a variable and replace the value of a setting that I want to change with the variable value.
#!/bin/bash
mysqlurlenv1="jdbc:mysl://xx.xx.xx.xx:3306
mysqlurlenv2="jdbc:mysl://xx.xx.xx.xz:3306
configFile=app.conf
pattern=$1
case "$pattern" in
1)
sed -i.bak "s/^\(mysql.db.url =\).*/\1 ${mysqldburl_env1}/"
;;
2)
sed -i.bak "s/^\(mysql.db.url =\).*/\1 ${mysqldburl_env2}/"
;;
*)
echo "Wrong input...try again"
esac
I have a config file with the following entry
mysql.db.driver = com.mysql.jdbc.Driver
mysql.db.url = jdbc:mysql....
mysql.db.uname = admin
When I run the script I got this error
sed: -e expression #1, char 40: unknown option to `s'
How to get those defined variable in the config file ? Any help will appreciated
Rgds
Din
You are using this sed pattern s/FIND_STRING/REPLACE_STRING, that is the common way to do it.
But your REPLACE_STRING contains /, so your pattern will become s/FIND_STRING/jdbc:mysl://.... and that is invalid, because it contains too much /.
Sed accepts any character as field separator, so in your case you need change sed pattern field separator from / to something else, like |.
Example:
$ mysqlurlenv1="jdbc:mysl://xx.xx.xx.xx:3306"
$ echo "mysql.db.url = jdbc:mysql" | sed "s|^\(mysql.db.url =\).*|\1 ${mysqlurlenv1}|"
mysql.db.url = jdbc:mysl://xx.xx.xx.xx:3306

Sed an entire line with only the begin of the line

I have a config file with this line:
foo.bar = http://localhost/foo
I want to replace this line with a new url. For example:
foo.bar = http://new_url/bar
But I don't know the original url, I only know the name of the parameter.
I tried this:
sed "s!^foo.bar.*!foo.bar = http://new_url/bar!" test.ini
But it return an error
Use single quotes to prevent shell expansion:
sed -e 's!^foo.bar.*!foo.bar = http://new_url/bar!'
In bash double-quotes and ! don't go well when placed inside ( see HISTORY EXPANSION in man bash), change the separator to # (or) use single-quotes
sed 's#foo.bar.*#foo.bar = http://new_url/bar#' file
foo.bar = http://new_url/bar

sed single quotes

I've been banging into escaping single quote's problem using SED (Bash shell).
I need to make
$cfg['Servers'][$i]['password'] = '';
into
$cfg['Servers'][$i]['password'] = 'mypassword';
What I've tried is:
sed -i "s/$cfg['Servers'][$i]['password'] = '';/$cfg['Servers'][$i]['password'] = '$rootpassword';/g" /usr/share/phpmyadmin/libraries/config.default.bak
Which ends up really jumbling the line.
$cfg['Servers'][$i]['password['Servers'][]['passsword'] = 'mypassword'
I've tried the '\'' to escape single quotes and I think everything else under the sun but just can't get it quite there.
can anyone point to my probable obvious mistake?
Thank you.
instead of escaping, you can use \x27 for single quote. this works not only for sed, for awk etc. as well.
see test:
kent$ cat tt
$cfg['Servers'][$i]['password'] = ''
kent$ sed -r 's/(\$cfg\[\x27Servers\x27\]\[\$i\]\[\x27password\x27\] \= \x27)\x27/\1mypassword\x27/g' tt
$cfg['Servers'][$i]['password'] = 'mypassword'
note that, the sed line may not be the best solution for your problem, e.g. put the whole string to "s/../" may not be necessary. However, I just want to show how the \x27 worked.
$ sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '';/\$cfg['Servers'][\$i]['password'] = '\$rootpassword';/g" file
Try this:
sed -i "/Servers.*password.*''$/s/''/'foo'/" /path/to/your/testfile
Match a line that contains "anything..Servers..anything..password..anything..''" (end with '') and on that line replace '' with 'foo'
This can match more than one lines, but only the first occurance will be changed.
Test it, it's most probable that .. Servers .. password .. '' is only on one line.
Or you can just escape everything.

Resources