Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell commands to match key value pairs

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?

like image 460
live2dream95 Avatar asked Feb 25 '10 15:02

live2dream95


People also ask

How do I use key-value pairs in bash?

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.

What does $() mean in shell?

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).

What is $_ in shell script?

$_ (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.

What does && do in shell script?

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.


2 Answers

what about

grep "key" my_file | cut -d'=' -f2
like image 130
Rob Wells Avatar answered Sep 28 '22 03:09

Rob Wells


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"=".

like image 27
Ethan Post Avatar answered Sep 28 '22 02:09

Ethan Post