使用字符串中的特殊字符并使用一个变量的Sed

huangapple go评论52阅读模式
英文:

Sed with special characters in the string and using a variable

问题

我尝试使用sed(带有-i选项)通过一个bash脚本来替换配置文件中的密码。

我的配置文件如下:

$DB['SERVER']                   = 'localhost';
$DB['PORT']                             = '0';
$DB['PASSWORD']                 = '';

我需要更新这些值,通过传递带有特殊字符(. - /等等)的变量。

我需要得到这个结果:

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';

我尝试了以下但没有成功。

sudo sed -i "s/$DB\\['PORT'\\]/\\/\\/\\/$DB\\['PORT'\\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "s/$DB\\['SERVER'\\]/\\/\\/\\/$DB\\['SERVER'\\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "s/$DB\\['PASSWORD'\\]/\\/\\/\\/$DB\\['PASSWORD'\\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "/^.*Zabbix GUI configuration file.*/a $DB['PORT']='3306';" /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file.*/a $DB['SERVER']='"${db_instance_address}"';' /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file.*/a $DB['PASSWORD']='"${db_password}"';' /etc/zabbix/web/zabbix.conf.php

输出结果如下:

egrep "SERVER|PORT|PASSWORD" /etc/zabbix/web/zabbix.conf.php

$DB['PASSWORD']='';
$DB['PORT']='3306';
$DB['SERVER'] = '';
//$DB['SERVER']                   = 'localhost';
//$DB['PORT']                            = '0';
//$DB['PASSWORD']                        = '';

感谢您的帮助。

英文:

I'm try to use sed (with -i option) to replace password in a config file by a bash script.

My config file is:

$DB['SERVER']                   = 'localhost';
$DB['PORT']                             = '0';
$DB['PASSWORD']                 = '';

I need to update these values passing variables with special characters (. - / etc...)

I need to have this result:

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';

I tried this but doesn't work.

sudo sed -i "s/$DB\\['PORT'\\]/\\/\\/$DB\\['PORT'\\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "s/$DB\\['SERVER'\\]/\\/\\/$DB\\['SERVER'\\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "s/$DB\\['PASSWORD'\\]/\\/\\/$DB\\['PASSWORD'\\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "/^.*Zabbix GUI configuration file.*/a $DB['PORT']='3306';" /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file.*/a $DB['SERVER']='"${db_instance_address}"';' /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file.*/a $DB['PASSWORD']='"${db_password}"';' /etc/zabbix/web/zabbix.conf.php

the output is:

egrep "SERVER|PORT|PASSWORD" /etc/zabbix/web/zabbix.conf.php

$DB['PASSWORD']='';
$DB['PORT']='3306';
$DB['SERVER'] = '';
//$DB['SERVER']                   = 'localhost';
//$DB['PORT']                            = '0';
//$DB['PASSWORD']                        = '';

Thanks for your help.

答案1

得分: 1

我建议您使用gnu-awk而不是sed,因为密码字符串可以包含可能在sed的替换字符串中引起问题的任何字符:

awk -i inplace -v ins="数据库实例地址" -v pw="数据库密码" -v port="数据库端口" '
BEGIN {FS=OFS="=";}
index($1, "$DB['SERVER") {
   $2 = " '" ins "';";
}
index($1, "$DB['PASSWORD") {
   $2 = " '" pw "';";
}
index($1, "$DB['PORT") {
   $2 = " '" port "';";
} 1' 文件

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
英文:

I would suggest you to use gnu-awk instead sed since a password string can contain any character that might create issues in a sed's replacement string:

awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47SERVER") {
$2 = "
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47" ins "
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47;"
} index($1, "$DB[
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47PASSWORD") {
$2 = "
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47" pw "
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47;"
} index($1, "$DB[
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47PORT") {
$2 = "
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47" port "
awk -i inplace -v ins="$db_instance_address" -v pw="$db_password" -v port="$db_port" '
BEGIN {FS=OFS="="}
index($1, "$DB[\047SERVER") {
   $2 = " \047" ins "\047;"
}
index($1, "$DB[\047PASSWORD") {
   $2 = " \047" pw "\047;"
}
index($1, "$DB[\047PORT") {
   $2 = " \047" port "\047;"
} 1' file

$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';
47;"
} 1' file $DB['SERVER'] = 'mydb.lab.local'; $DB['PORT'] = '3306'; $DB['PASSWORD'] = 'Passw0rd!';

答案2

得分: 0

感谢大家的支持。

我用这段代码解决了这个问题:

sudo sed -i "s/$DB\['PORT'\]/\/\/$DB\['PORT'\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "s/$DB\['SERVER'\]/\/\/$DB\['SERVER'\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "s/$DB\['PASSWORD'\]/\/\/$DB\['PASSWORD'\]/g" /etc/zabbix/web/zabbix.conf.php
sudo sed -i "/^.*Zabbix GUI configuration file./a $DB['PORT'] = '3306';" /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file./a $DB[\'SERVER\'] = \'\''"${db_instance_address}"'\'';' /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file./a $DB[\'PASSWORD\'] = \'\''"${db_password}"'\'';' /etc/zabbix/web/zabbix.conf.php

输出结果为:

$DB['PASSWORD'] = 'Passw0rd!';
$DB['SERVER'] = 'mydb.lab.local';
$DB['PORT'] = '3306';
//$DB['SERVER']                 = 'localhost';
//$DB['PORT']                           = '0';
//$DB['PASSWORD']                       = '';

最佳解决方案是替换现有行,但我无法这样做,所以我将其注释并创建了一行新的。

英文:

Thanks to all for the support.

I solved with this code:

sudo sed -i "s/$DB\\['PORT'\\]/\\/\\/$DB\\['PORT'\\]/g" /etc/zabbix/web/zabbix.conf.php 
sudo sed -i "s/$DB\\['SERVER'\\]/\\/\\/$DB\\['SERVER'\\]/g" /etc/zabbix/web/zabbix.conf.php 
sudo sed -i "s/$DB\\['PASSWORD'\\]/\\/\\/$DB\\['PASSWORD'\\]/g" /etc/zabbix/web/zabbix.conf.php 
sudo sed -i "/^.*Zabbix GUI configuration file.*/a $DB['PORT'] = '3306';" /etc/zabbix/web/zabbix.conf.php 
sudo sed -i '/^.*Zabbix GUI configuration file.*/a $DB[\'\''SERVER\'\''] = \'\'''"${db_instance_address}"'\'\'';' /etc/zabbix/web/zabbix.conf.php
sudo sed -i '/^.*Zabbix GUI configuration file.*/a $DB[\'\''PASSWORD\'\''] = \'\'''"${db_password}"'\'\'';' /etc/zabbix/web/zabbix.conf.php

The output is:

$DB['PASSWORD'] = 'Passw0rd!';
$DB['SERVER'] = 'mydb.lab.local';
$DB['PORT'] = '3306';
//$DB['SERVER']                 = 'localhost';
//$DB['PORT']                           = '0';
//$DB['PASSWORD']                       = '';

The best solution is to replace existing row, but I can't do it, and I comment and create a new row.

答案3

得分: 0

使用 sed 命令可以将以下部分翻译为中文:

$ sed -E "/^$DB/{
    /SERVER/s/'[^']*'/${db_server}/3
    /PORT/s/'[^']*'/${db_port}/3
    /PASSWORD/s/'[^']*'/${db_password}/3
}" /etc/zabbix/web/zabbix.conf.php
$DB['SERVER'] = 'mydb.lab.local';
$DB['PORT'] = '3306';
$DB['PASSWORD'] = 'Passw0rd!';

请注意,我只翻译了代码部分,其他部分不包括在内。

英文:

Using sed

$ sed -E "/^$DB/{
    /SERVER/s/'[^']*/'${db_server}/3
    /PORT/s/'[^']*/'${db_port}/3
    /PASSWORD/s/'[^']*/'${db_password}/3
}" /etc/zabbix/web/zabbix.conf.php
$DB['SERVER']                   = 'mydb.lab.local';
$DB['PORT']                             = '3306';
$DB['PASSWORD']                 = 'Passw0rd!';

huangapple
  • 本文由 发表于 2023年6月12日 22:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定