I want to print an intervening section (between a start pattern and an end pattern) from a file, with particular lines getting coloured.
Here is a sample text in one such file
## Beginning of file
Some text and code
## FAML [ASMB] KEYWORD
## Some information.
## Some other text.
## Blu:
## Some text in blue.
## END OF FAML [ASMB]
## Other text
More text and code
The text between ## FAML [ASMB] KEYWORD and ## END OF FAML [ASMB] is to be extracted (without the beginning ##) and passed to the function luciferin which will print the multiline text appropriately.
The text between blocks is discarded. Subsequent blocks work the same, the intervening region is extracted and printed by calling the function luciferin(rec). The function luciferin does the output in colour.
The input string for luciferin would be
Some information.
Some other text.
Blu:
Some text in blue.
Here is the awk script that captures the intervening region
BEGIN {
beg_ere = "## [[:alnum:]]+ [[][[:alnum:]]+[]]"
end_ere = "## END OF [[:alnum:]]+ [[][[:alnum:]]+[]]"
}
match($0, beg_ere, paggr) { display = 1 }
$0 ~ end_ere { display = 0 ; next }
display { print }
Here is the luciferin function that takes a string for output in colour. Where cpt in the colour escape sequence, and astr[i] is a particular line i of the multiline input string.
function luciferin(mstr) {
cpt = tseq["Grn:"]
nlines = split(mstr, astr, "\n")
for (i = 1; i <= nlines; i++) {
for ( knam in tseq ) {
if ( knam == astr[i] ) { cpt = tseq[knam] ; break }
}
if (knam == str) { print "" } else { print cpt astr[i] rst }
}
}
sed '/start-pattern/,/end-pattern/!d'or something?