I'm trying to extract the value of keys entered as options for a command-line bash script and failing.
I've tried matching regex with grep and sed both with and without the -E option. I can get a grep to work extracting keys from the initial command input:
grep -wo -- "-[^ ]*"
Works to extract keys from the command line that have "-"
or "--"
format, but I need the values between two keys or from a key to the end of line if there's no additional key(s).
key="-r"
input="command -r root1 root2 -s ip"
echo $input | sed "s/^.*$key \([^-]\+\)\( -.\+$\|$\)/$key has value \1/g;"
Output:
command -r root1 root2 -s ip
Expected output:
root1 root2
Clearly, \1
is matching the whole line, but I just need the contents of the input line from the $key
to the next key or the end of line. Any assistance would be appreciated.