I have to parse a file, kind of a config file, that is adopting a really simple grammar : there are 2 kind of multiline blocks and inline markup tags .
the block of type 1 is marked by an open_tag and a closing_tag plus the name of the block, example
START_BLOCK_1 name
< content >
END_BLOCK_1
blocks of type 1 only contain blocks of type 2, they are basically used to switch on or off blocks of this configuration file .
the block of type 2 is marked by a tag at the start of a new line ( plus the name ) and there is non closing tag, the closing is implicitly made when a new block starts or when the file ends, the block itself is allowed to contain empty lines .
START_BLOCK_2 name_1
< content >
< content >
< content >
START_BLOCK_2 name_2
< content >
< content >
< content >
START_BLOCK_2 name_3
< content >
< content >
the final kind of token is just an inline tag, a special word that is present at the start of the line and I just to get what is the value marked by that tag
START_BLOCK_2 name_1
tag_1 red
tag_2 Jon
START_BLOCK_2 name_2
tag_1 blue
tag_2 Phil
A good final example could be
START_BLOCK_2 name_1
< content >
START_BLOCK_2 name_2
< content >
START_BLOCK_1 name_1
START_BLOCK_2 name_3
< content >
START_BLOCK_2 name_4
< content >
END_BLOCK_1
START_BLOCK_2 name_5
< content >
Considering the names of blocks of type 2 I need to know the values associated to each tag ( if they contain tags that are set ) and if they are part of type 1 blocks and in this case the name attribute of the type 1 block that contains them .
The result could be stored in a file or printed out, as long as I can parse this I can always re-read the output later in a formatted way .
It's relatively simple to parse this file but I have never done such thing with just a GNU/linux shell and I would like to know if this is possible and the names of the tools made for this .
EDIT
input
START_BLOCK_2 opt1
color red
START_BLOCK_1 opt2
START_BLOCK_2 opt3
name Jon
START_BLOCK_2 opt4
color blu
END_BLOCK_1
expected output
opt1 red
opt3 opt2
opt3 Jon
opt4 opt2
opt4 blu
awk
, in general parsing problems do better with real parsers, and so I'd usually write one in another language like python or perl – Eric Renouf Nov 18 '16 at 18:06awk
do this, or you could even do it in the shell directly with awhile
loop. – Eric Renouf Nov 18 '16 at 18:11