Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1053
  • Last Modified:

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!


0
pennypiper
Asked:
pennypiper
  • 2
  • 2
1 Solution
 
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
 
ozoCommented:
awk -F'"'  '/\[name=/{n=$2};/\[TYPE/{print n " \"" $2 "\""}' inputfile
0
 
pennypiperAuthor Commented:
That's what I was looking for.  Thanks ozo!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now