I have a file that I need to look up a value by key using a shell script. The file looks like:
HereIsAKey This is the value
How can I do something like:
MyVar=Get HereIsAKey
and then MyVar should equal "This is the value". The key has no whitespace and the value should be everything following whitespace after the key.
if HereIsAKey
is unique in your file, try this with grep:
myVar=$(grep -Po "(?<=^HereIsAKey ).*" file)
If you don't have a grep that supports Perl-compatible regular expressions, the following seems to work:
VAR=$(grep "^$KEY " file | cut -d' ' -f2-)
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