场景:
用shell脚本,从A文件的获取URL参数,覆盖B文件的URL
A文件内容
http://appldnld.apple.com/ios8.4.1/031-31019-20150812-75169310-3C8F-11E5-BAB2-9B1A3A53DB92/iPad4,8_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31157-20150812-7518D8FA-3C8F-11E5-B354-AD1A3A53DB92/iPad4,9_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31386-20150812-A4FC2602-3C90-11E5-B036-A21D3A53DB92/iPad5,3_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31164-20150812-7519C21A-3C8F-11E5-937E-AF1A3A53DB92/iPad5,4_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31129-20150812-751A3CB8-3C8F-11E5-A8A5-A91A3A53DB92/iPhone4,1_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31186-20150812-751D243C-3C8F-11E5-8E4F-B51A3A53DB92/iPhone5,1_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31065-20150812-7518F132-3C8F-11E5-A96A-A11A3A53DB92/iPhone5,2_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31126-20150812-751B1656-3C8F-11E5-8F6B-A71A3A53DB92/iPhone5,3_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-30941-20150812-7516948C-3C8F-11E5-B90B-951A3A53DB92/iPhone5,4_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31174-20150812-75196C52-3C8F-11E5-8C71-B31A3A53DB92/iPhone6,1_8.4.1_12H321_Restore.ipsw
http://appldnld.apple.com/ios8.4.1/031-31002-20150812-7517E210-3C8F-11E5-A4C6-991A3A53DB92/iPhone6,2_8.4.1_12H321_Restore.ipsw
B文件内容
[iPhone6,2]
Name=iPhone 5S
Model=Global
Version=8.4.1
ProductIDs=33589600
URL=http://appldnld.apple.com/ios8.4.1/031-31002-20150812-7517E210-3C8F-11E5-A4C6-991A3A53DB92/iPhone6,2_8.4.1_12H321_Restore.ipsw
[iPhone5,1]
Name=iPhone 5
Model=GSM
Version=8.4.1
ProductIDs=35152
URL=http://appldnld.apple.com/ios8.4.1/031-31186-20150812-751D243C-3C8F-11E5-8E4F-B51A3A53DB92/iPhone5,1_8.4.1_12H321_Restore.ipsw
[iPhone5,2]
Name=iPhone 5
Model=Global
Version=8.4.1
ProductIDs=33589584
URL=http://appldnld.apple.com/ios8.4.1/031-31065-20150812-7518F132-3C8F-11E5-A96A-A11A3A53DB92/iPhone5,2_8.4.1_12H321_Restore.ipsw
方案:
看到两个文件中的URL都包含唯一的iOS设备名称。以它作为主键来匹配URL。用awk分别取出A,B文件的对应URL,用sed做替换。
步骤:
- 用awk 提取A,B文件中包含”iPhone5,1 “这个字符的URL
awk 的用法很简单 awk ‘/匹配规则/ {print(跟C语言类似的格式化输出)} ’ + 数据源文件
awk ‘/iPhone5,1/ {printf(“iPhone51_expect=%s”, $1)}’ A.txt
awk ‘/iPhone5,1/ && /ipsw/ {printf(“iPhone51_target=%s”, $1)}’ B.txt
这里因为B文件iPhone5,1 会同时匹配出URL所在行的数据和设备名所在行的数据,所以增加一个/ipsw/的过滤,双重匹配
- 用sed替换
sed -i ” “s@$iPhone51_target@URL=$iPhone51_expect@g” B.txt
这里注意几点:
* 用@符合作为sed替换命令的分隔符
* 用 -i 命令将替换结果写入文件
* -i 之后的’’ 表示不生成备份文件,解决报错问题
- 等替换完成后,可以再使用awk命令将两个文件的URL数据都提取出来并排序。然后用diff命令验证数据是否一致
get iOSCfg ipsw list
cat B.txt | grep ipsw | grep 8.4.1 | sort -b -u | sort -k 1.93 | sed ‘s/<string>//g’ | sed ‘s/</string>//g’ | grep -v protected | sed ‘s/URL=http/http/g’ > pc_ipsw_list.txt
get mac plist ipsw list
cat A.txt | sed ‘s/<string>http/http/g’ | sed ‘s/</string>/ /g’ | awk ‘/8.4.1/’ |grep ipsw | sed ‘s/^[[:space:]]//’ | sed ‘s/[ \t]$//g’ | sort -k 1.93 > mac_ipsw_list.txt
diff mac_ipsw_list.txt pc_ipsw_list.txt