查找并替换行使用变量sed


sn=$(./libs/ideviceinfo | grep ^SerialNumber | awk {'printf $NF'})
type=$(./libs/ideviceinfo | grep ProductType | awk {'printf $NF'})
udid=$(./libs/ideviceinfo | grep UniqueDeviceID | awk {'printf $NF'})

我想把变量value替换成这个txt文件

{
"InternationalMobileEquipmentIdentity" = "355331088790894";
"SerialNumber" = "C6KSJM0AHG6W";
"InternationalMobileSubscriberIdentity" = "";
"ProductType" = "iPhone9,1";
"UniqueDeviceID" = "69bae2fcc0da3e6e3373f583ef856e02c88026eb";
"ActivationRandomness" = "25E7742B-76A7-4C31-9F49-52D17A817B2F";
"ActivityURL" = "https://albert.apple.com/deviceservices/activity";
"IntegratedCircuitCardIdentity" = "";
"CertificateURL" = "https://albert.apple.com/deviceservices/certifyMe";
"PhoneNumberNotificationURL" = "https://albert.apple.com/deviceservices/phoneHome";
"ActivationTicket" = "";
}

我尝试使用sed:

sed 's/"SerialNumber.*/"SerialNumber" = "$sn";/g' ./file/bp.txt > ./file/bp1.txt

输出不符合预期:"SerialNumber" = "$sn";

希望你们能帮助我

p/s:如果一个命令可以同时替换3个变量值,那就太好了

这里的问题是shell引用之一。使用单引号意味着里面的所有内容都不会被替换。

下面的代码应该可以解决你的问题:

sed 's/"SerialNumber.*/"SerialNumber" = "'"$sn"'";/g' ./file/bp.txt > ./file/bp1.txt

最新更新