Awk command for multi-line regex

Hello,

Assume an input file like this:

[name="n1"]
[name="n2"]

[TYPE description="description one"]

[name="n3"]
[TYPE description="description two"]

I need to assocate the description field of a [TYPE ] line with the name of the [name=] field which immediately precedes it in the file.  In the above example, for instance, the name "n2" would be associated with "description one" and "n3" would be associated with "description two".  There would be nothing associated with name "n1".  There may be zero or more blank lines between the [name] line and the [TYPE] line.

The output (to stdout) would look like the following:
n1 "description one"
n2 "description two"

Is this possible with Awk?

Thanks!


pennypiperAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
awk -F'"'  '/\[name=/{n=$2};/\[TYPE/{print n " \"" $2 "\""}' inputfile
0
 
ozoCommented:
> I need to assocate the description field of a [TYPE ] line with the name of the [name=] field which immediately precedes it in the file.  In the above example, for instance, the name "n2" would be associated with "description one" and "n3" would be associated with "description two".There would be nothing associated with name "n1".

This seems to contradict
> The output (to stdout) would look like the following:
n1 "description one"
n2 "description two"

Which is correct?  Or am I misinterpreting something?
0
 
pennypiperAuthor Commented:
Oops, typo.  You're right it should be:

n2 "description one"
n3 "description two"

Sorry about that.
0
 
pennypiperAuthor Commented:
That's what I was looking for.  Thanks ozo!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.