使用sed搜索和替换

时间:2016-06-07 09:46:22

标签: bash shell awk sed

我在/ tmp位置有一个名为config.ini的文件。 具有以下内容。

[vivek@localhost ~]$ cat /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecurity\webserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$ADMIN_PASSWD"
"adminuser"="admin"
[vivek@localhost ~]$

我需要更换" adminsyspassword"使用/ etc / shadow文件值中的值。我使用下面的shell脚本和awk命令来获取" adminsyspassword"的新值。 ini文件的密钥。

[vivek@localhost ~]$ cat /tmp/a.sh
#! /bin/sh

echo "Fetching the admin encrypted Password from the /etc/shadow file"
ADMIN_PASSWD=`sudo awk -F: '/admin/ { print $2}' /etc/shadow`

PARAM_TO_CHANGE='^\(\"adminsyspassword\"\=\)'
ANYTHING='.*'
SEARCH_PATTERN=${PARAM_TO_CHANGE}${ANYTHING}
REPLACE_PATTERN="\"${ADMIN_PASSWD}\""
sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini
[vivek@localhost ~]$

假设通过awk命令接收的值存储在ADMIN_PASSWD变量中。并且以调试格式运行shell脚本时捕获$ ADMIN_PASSWORD的值。

[vivek@localhost ~]$ bash -x /tmp/a.sh
+ echo 'Fetching the admin encrypted Password from the /etc/shadow file'
Fetching the admin encrypted Password from the /etc/shadow file
++ sudo awk -F: '/admin/ { print $2}' /etc/shadow
+ ADMIN_PASSWD='$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0'
+ PARAM_TO_CHANGE='^\(\"adminsyspassword\"\=\)'
+ ANYTHING='.*'
+ SEARCH_PATTERN='^\(\"adminsyspassword\"\=\).*'
+ REPLACE_PATTERN='"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"'
+ echo 'sed '\''s|^\(\"adminsyspassword\"\=\).*|\1\"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|'\'' /tmp/config.ini'
sed 's|^\(\"adminsyspassword\"\=\).*|\1\"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|' /tmp/config.ini
+ sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecurity\webserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$ADMIN_PASSWD"
"adminpassword"="MD5-71293d5fd498273ca795a80ea9fa73c7"
"adminuser"="admin"
[vivek@localhost ~]$

我无法理解为什么sed命令对我不起作用。

我哪里错了?

更新

我尝试在shell脚本中添加echo命令,以查看实际传递的内容。

echo "sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini"
sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini

现在扩展版本正在运行,但是如何在shell脚本中创建呢?

[vivek@localhost ~]$ sed 's|^\(\"adminsyspassword\"\=\).*|\1\"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|' /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecurity\webserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"
"adminpassword"="MD5-71293d5fd498273ca795a80ea9fa73c7"
"adminuser"="admin"
[vivek@localhost ~]$

感谢您的帮助。 我得到了解决方案,需要改变这种格式。

[vivek@localhost ~]$ cat /tmp/a.sh
#! /bin/sh

echo "Fetching the admin encrypted Password from the /etc/shadow file"
ADMIN_PASSWD=`sudo awk -F: '/admin/ { print $2}' /etc/shadow`

PARAM_TO_CHANGE='^\(\"adminsyspassword\"\=\)'
ANYTHING='.*'
SEARCH_PATTERN=${PARAM_TO_CHANGE}${ANYTHING}
REPLACE_PATTERN="\"${ADMIN_PASSWD}\""

sed "s|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|" /tmp/config.ini

1 个答案:

答案 0 :(得分:0)

问题是在调用sed命令时使用单引号会阻止shell扩展SEARCH_PATTERNREPLACE_PATTERN shell变量。这导致这些字符串按字面顺序传递给sed程序。

sed 's|$SEARCH_PATTERN|\\1$REPLACE_PATTERN|' /tmp/config.ini

要使shell执行参数扩展,应删除单引号或用双引号替换。我会简化sed命令以在$ADMIN_PASSWD中搜索文字字符串/tmp/config.ini,并将其替换为ADMIN_PASSWD shell变量的内容:

sed s/\$ADMIN_PASSWD/$ADMIN_PASSWD/ /tmp/config.ini