简体   繁体   中英

shell script to get key value

I have content like

key1="value1" key2="value2"
key1="value11" key2="value22"
key1="value111" key2="value222"

I want to output like

value1
value11
value111

ie basically values for key one

but when I grep the entire line will be shown, I tried using cut still could not get expected result, can some help me to write scrip for this please

Using awk you can search for given key like this:

awk -v s="key1" -F '[= "]+' '{for (i=1; i<NF; i+=2) if ($i==s) print $(i+1)}' file
value1
value11
value111

awk -v s="key2" -F '[= "]+' '{for (i=1; i<NF; i+=2) if ($i==s) print $(i+1)}' file
value2
value22
value222

Using cut itself:

cut -d \" -f 2 < File

Set " as delimiter and extract the 2nd field. Hope it helps.

Another similar solution with awk :

awk -F\" '{print $2}' File

Using grep -P :

$ grep -oP '(?<=key1=")[^"]*' file
value1
value11
value111

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM