Search code examples
bashvariablesreplacefindline

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'})

I want to replace variable value into this txt file

{
    "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" = "";
}

i try using sed:

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

The output is not as expected: "SerialNumber" = "$sn";

Hope you guys can help me

p/s: can you help me if 1 command can replace 3 variable values ​​at the same time, that would be great


Solution

  • The problem here is one of shell quoting. Using single quotes means that everything inside will not go through substitution.

    The following should fix your problem:

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