So I want to create script to update a version number.
The file contents look like this. I struggle to extract the number from the file. I want to extract and parse the number "58" The other numbers might also change. The file contents look like this.:
# Some more lines above
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '0.2.58'
# More lines below
My approach was to use grep -o Alamofire.git.*[0-9].[0-9].[0-9] ../Podfile
But this doesn't work. Is there any easy solution to this?
CodePudding user response:
If you can use ggrep
ggrep -oP 'Alamofire\.git.*[0-9]\.[0-9]\.\K[0-9] ' ../Podfile
Output
58
Another option with gnu-awk
and a capture group:
gawk 'match($0, /Alamofire\.git.*[0-9]\.[0-9]\.([0-9] )/, a) {print a[1]}' ../Podfile
CodePudding user response:
With your shown samples and in any awk
version please try following
awk
code. Here is the Online demo for used regex (^[[:space:]] pod \047Alamofire\047, :git => \047https?:\/\/github\.com\/.*\/Alamofire\.git\047, :tag => \047([0-9] \.) [0-9] \047
) to get needed values by 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
CodePudding user response:
With GNU Grep:
grep -o -P 'Alamofire.*:tag.*\.\K\d ' file
Sed:
sed -nE 's/.*Alamofire.*\.([[:digit:]]*).*/\1/p' file
Awk:
awk -F. '/Alamofire/{gsub(/[^[:digit:]]/,"", $NF); print $NF}' file
POSIX grep native on MacOS you can use a pipe:
grep 'Alamofire' file | tr -d '"' | rev | cut -d'.' -f 1 | rev