AhmedMItman
AhmedMItman

Reputation: 239

Bash Script to get Value of Key from file

I have a file that contain list of users and his token value like :

- name: A@X/xxxxxx:123
  user:
    token: abcdefghijk
- name: B@Y/yyyyyyy:456
  user:
    token: dassafsdfscczcz

I want to get token value for specific name using bash script ?

Upvotes: 0

Views: 521

Answers (2)

Nic3500
Nic3500

Reputation: 8621

You can use grep:

grep -A 2 "A@X/xxxxxx:123" THEDATAFILE | grep token | cut -d':' -f2

It will work even if you have the same user twice and print both tokens.

Tested with GNU grep on Linux.

Upvotes: 1

李伟杰
李伟杰

Reputation: 36

$ param="A@X"
$ awk '{if($2~/name/) {a=$3} else {print a,$0}}' temp |grep token|grep $param|awk '{print $3}'
abcdefghijk

Upvotes: 2

Related Questions