場(chǎng)景:
用shell腳本首繁,從A文件的獲取URL參數(shù)垛玻,覆蓋B文件的URL
A文件內(nèi)容
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文件內(nèi)容
[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
方案:
看到兩個(gè)文件中的URL都包含唯一的iOS設(shè)備名稱冀痕。以它作為主鍵來匹配URL甩卓。用awk分別取出A填渠,B文件的對(duì)應(yīng)URL施掏,用sed做替換帖烘。
步驟:
- 用awk 提取A亮曹,B文件中包含”iPhone5,1 “這個(gè)字符的URL
awk 的用法很簡(jiǎn)單 awk ‘/匹配規(guī)則/ {print(跟C語言類似的格式化輸出)} ’ + 數(shù)據(jù)源文件
awk '/iPhone5,1/ {printf("iPhone51_expect=%s", $1)}' A.txt
awk '/iPhone5,1/ && /ipsw/ {printf("iPhone51_target=%s", $1)}' B.txt
這里因?yàn)锽文件iPhone5,1 會(huì)同時(shí)匹配出URL所在行的數(shù)據(jù)和設(shè)備名所在行的數(shù)據(jù),所以增加一個(gè)/ipsw/的過濾秘症,雙重匹配
- 用sed替換
sed -i '' "s@$iPhone51_target@URL=$iPhone51_expect@g" B.txt
這里注意幾點(diǎn):
* 用@符合作為sed替換命令的分隔符
* 用 -i 命令將替換結(jié)果寫入文件
* -i 之后的’’ 表示不生成備份文件照卦,解決報(bào)錯(cuò)問題
- 等替換完成后,可以再使用awk命令將兩個(gè)文件的URL數(shù)據(jù)都提取出來并排序乡摹。然后用diff命令驗(yàn)證數(shù)據(jù)是否一致
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