I have a file where I basically need grep to return the "key" associated with a particular value.
For instance, if I have:
[abc]
field1=34234
field2=t2342
[def]
field1=44354
field2=23423
I want to be able to grep, say, 23423, and then basically match on [def]
and return/print out the contents of the brackets (e.g. def
).
The file is formatted something like this, where I want to match the first line containing [
and ]
before the argument to grep, if that makes sense.
I found this question, which is similar, where -B 1
will give you the line before, and some variant of that can be used to prevent some number of lines before the match.
The problem is I can't count on the exact number of lines between the match and the first line with brackets before it, so I want to avoid doing that and simply match for the first line containing a bracket before.
Is this possible to do with grep, or a similar bash utility?