I have a script called foo.sh that contains something like
exec myapp -o size=100m -f
Any idea how to create another script that parses foo.sh and retrieves the value of size? One can assume myapp only appears once in foo.sh, but the order of size argument can appear anywhere in the argument list
Thanks
With grep in a shell :
$ grep -oP 'myapp.*?size=\K\d+m' foo.sh
100m
With awk in a shell :
$ awk -F'size=' '{sub(/ -f/, "");print $2}' foo.sh
100m
or
$ awk '{print gensub(/.*size=([0-9]+m).*/, "\\1", $0)}' foo.sh
100m
With perl in a shell :
$ perl -lne 'print $1 if /exec.*?size=(\d+m)/' foo.sh
100m
Or using a shell funny trick :
$ declare $(grep -oP "\b\w+=\w+\b" foo.sh)
$ echo $size
100m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With