所以我想創建腳本來更新版本號。
檔案內容如下所示。我努力從檔案中提取數字。我想提取和決議數字“58”其他數字也可能會改變。檔案內容如下所示:
# Some more lines above
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '0.2.58'
# More lines below
我的方法是使用grep -o Alamofire.git.*[0-9].[0-9].[0-9] ../Podfile
但這不起作用。有什么簡單的解決方案嗎?
uj5u.com熱心網友回復:
使用您顯示的示例和任何 awk 版本,請嘗試以下
awk代碼。這是使用正則運算式 ( ) 的在線演示^[[:space:]] pod \047Alamofire\047, :git => \047https?:\/\/github\.com\/.*\/Alamofire\.git\047, :tag => \047([0-9] \.) [0-9] \047,以通過 OP 獲取所需的值。
awk '
match($0,/^[[:space:]] pod \047Alamofire\047, :git => \047https?:\/\/github\.com\/.*\/Alamofire\.git\047, :tag => \047([0-9] \.) [0-9] \047/){
val=substr($0,RSTART,RLENGTH)
gsub(/.*\.|\047$/,"",val)
print val
}
' Input_file
說明:為上述正則運算式添加詳細說明。
^[[:space:]] pod ##From starting of value matching space(s) followed by string pod followed by a space.
\047Alamofire\047, ##Matching ' followed by Alamofire followed by ' and comma here.
:git => \047https? ##Matching :git space => followed by ' http and keeping s optional(to match http also).
:\/\/github\.com\/ ##Matching colon // github.com followed by a / here.
.*\/Alamofire\.git ##Matching everything till / followed by Alamofire followed by .git here.
\047, :tag => \047 ##Matching ', followed by space :tag followed by space => followed by space and ' here.
([0-9] \.) ##Matching digits(1 or more occurrences) followed by dot and this whole group 1 or more times.
[0-9] \047 ##Matching 1 or more digits followed by a ' here.
uj5u.com熱心網友回復:
如果你可以使用ggrep
ggrep -oP 'Alamofire\.git.*[0-9]\.[0-9]\.\K[0-9] ' ../Podfile
輸出
58
另一個選項awk和更具體的匹配將.and設定'為欄位分隔符:
awk -F"[.']" '
match ($0, /Alamofire\.git.*\047[0-9] \.[0-9] \.[0-9] \047$/) {
print $(NF-1)
}' file
或與gnu-awk一個捕獲組:
gawk 'match($0, /Alamofire\.git.*[0-9]\.[0-9]\.([0-9] )/, a) {print a[1]}' ../Podfile
uj5u.com熱心網友回復:
使用 GNU Grep:
grep -o -P 'Alamofire.*:tag.*\.\K\d ' file
賽德:
sed -nE 's/.*Alamofire.*\.([[:digit:]]*).*/\1/p' file
呸:
awk -F. '/Alamofire/{gsub(/[^[:digit:]]/,"", $NF); print $NF}' file
MacOS 上的 POSIX grep 本機您可以使用管道:
grep 'Alamofire' file | tr -d [\'\"] | rev | cut -d'.' -f 1 | rev
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/507408.html
上一篇:?探秘 Web 水印技術
下一篇:macOS上的浮點二進制表示
