使用变量使用 sed 查找和替换行

find and replace line with variable use 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'})

我想将变量值替换到这个 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:如果1条命令可以同时替换3个变量值,你能帮帮我吗,那就太好了

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

以下应该可以解决您的问题:

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