Reputation: 11
I have a file containing the following data :
[abc]
p=xyz
q=123
r=nm
[stu]
p=hjk
q=1234
r=jk
I want to access the value of variable "p" which is inside the block"abc".
Can anybody help me in this? Thanks
Upvotes: 1
Views: 40
Reputation: 289625
Let's start checking lines once [abc]
is found and stop once another line starting with [
is found. In the meanwhile, check the first field being p
and, in that case, print the second one:
$ awk -F= '$1=="[abc]" {f=1; next} f && $1=="p" {print $2} f && /^\[/ {f=0}' file
xyz
Upvotes: 1
Reputation: 687
You can try this:
awk 'BEGIN{RS="["} $1=="abc]"{ print $2 }' <filename> | cut -d'=' -f2
This will give the value of p, i.e. xyz.
Upvotes: 0