简体   繁体   English

sed如何用字符串作为变量替换整行?

[英]how to sed replace whole line with the string as variable?

how to sed replace whole line with the string as variable ? sed如何用字符串作为变量替换整行?

#!/bin/bash
ssh $1 ssh-keyscan -t rsa $1 > /tmp/$1
RSA=$(cat /tmp/$1)
echo $RSA
sed -i 's:'^"$1".*':'"$RSA"':' /etc/ssh/ssh_known_hosts
cat /etc/ssh/ssh_known_hosts | grep $1

It is storing the variable in RSA but not replacing, not sure what's wrong with sed part. 它将变量存储在RSA中,但没有替换,不确定sed部分出了什么问题。

You can use the following command. 您可以使用以下命令。

#!/bin/bash
ssh $1 ssh-keyscan -t rsa $1 > /tmp/$1
RSA=$(cat /tmp/$1)
echo $RSA
sed -i -e "/$1/ d" -e "/^$1/ a $RSA" /etc/ssh/ssh_known_hosts
cat /etc/ssh/ssh_known_hosts | grep $1

I modified as per your requirement to add if not line exists and replace if it exists. 我根据您的要求进行了修改,如果不存在则添加行,如果存在则替换行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM