【问题标题】:find and replace line with variable use sed使用 sed 查找和替换行
【发布时间】:2021-10-06 11:24:31
【问题描述】:
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个变量值,你能帮帮我,那就太好了

【问题讨论】:

    标签: bash variables replace find line


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2018-10-01
      • 2017-11-11
      • 2015-01-04
      • 2020-06-24
      • 1970-01-01
      相关资源
      最近更新 更多