I am trying to use awk to parse a multiline expression. A single one of them looks like this:
_begin hello world !
_attrib0 123
_attrib1 super duper
_attrib1 yet another value
_attrib2 foo
_end
I need to extract the value associated to _begin and _attrib1. So in the example, the awk script should return (one per line):
hello world ! super duper yet another value
The separator used is a tab (\t) character. Spaces are used only within strings.
The following awk script does the job:
You didn’t specify whether you want a tab (
\t) to be your output field separator. If you do, let me know and I’ll update the answer. (Or you can; it’s trivial.)Of course, if you want a scary alternative (since we’re getting close to Hallowe’en), here a solution using
sed:How does this work? Mwaahahaa, I’m glad you asked.
/^_begin./{s///;h;};— When we see_begin, strip it off and store the rest of the line to sed’s “hold buffer”./^_attrib1[^0-9]/{s///;H;x;s/\n/ /;x;};— When we see_attrib1, strip it off, append it to the hold buffer, swap the hold buffer and pattern space, replace newlines with spaces, and swap the hold buffer and pattern space back again./^_end/{;g;p;}— We’ve reached the end, so pull the hold buffer into the pattern space and print it.This assumes that your input field separator is just a single tab.
SO simple. Who ever said
sedwas arcane?!