I've got a file that contains a list of key=value
pairs, each on its own line. What's the best way to fetch the value for a specified key using shell commands?
You can run the ssh-keygen utility on your local machine to generate an SSH key-value pair. The ssh-keygen utility generates a pair comprising of a public key and private key. The private key is stored in the file id_rsa while the public key is stored in the file id_rsa.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
Logical AND operator(&&):The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.
what about
grep "key" my_file | cut -d'=' -f2
This is how I do it in ksh.
FOO=$(grep "^key=" $filename | awk -F"=" '{print $2}')
You can also use cut instead of awk. If you delimit the key pair with a space you can drop the -F"=".
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