Skip to main content
5 of 5
deleted 122 characters in body
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Since there's neither a minimal complete example of code, nor adequate sample input/output to test with, this is obviously just an untested guess but it looks like you should change:

display { print }

to

display { rec = rec $0 ORS }

and

$0 ~ end_ere { display = 0 ; next }

to

$0 ~ end_ere { luciferin(rec); rec = ""; display = 0 ; next }

or similar and tweak luciferin to remove the additional trailing newline from it's arg before printing.


Regarding how the question and the OPs questions in general could be improved - here's what a complete, minimal code sample would look like in a question such as this one:

$ cat tst.awk
$2 == "FAML" { display = 1 ; next }
$2 == "END" { display = 0 ; next }
display { print }

function luciferin(mstr) {
    nlines = split(mstr, astr, "\n")
    for (i = 1; i <= nlines; i++) {
        print "Luci:", astr[i]
    }
}

and some sample input to demonstrate your needs and test with:

$ cat input
## Beginning of file

Some text and code

## FAML [ASMB] KEYWORD
##  Some information.
##  Some other text.
## END OF FAML [ASMB]

## Other text

## FAML [ASMB] KEYWORD
##  Some other information.
##  Even more text.
## END OF FAML [ASMB]

More text and code

and the expected output given that input:

Luci: ##  Some information.
Luci: ##  Some other text.
Luci: ##  Some other information.
Luci: ##  Even more text.

The fact that your real code does coloring or whatever else is utterly irrelevant to the problem you want help with which is simply how to store a block of text and call luciferin() to print it modified in some way.

Given a clear, simple example like that we can very quickly show you a solution, e.g.:

$ cat tst.awk
$2 == "FAML" { display = 1 ; next }
$2 == "END" { luciferin(rec); rec = ""; display = 0 ; next }
display { rec = rec $0 ORS }

function luciferin(mstr) {
    nlines = split(mstr, astr, "\n")
    for (i = 1; i < nlines; i++) {
        print "Luci:", astr[i]
    }
}

$ awk -f tst.awk input
Luci: ##  Some information.
Luci: ##  Some other text.
Luci: ##  Some other information.
Luci: ##  Even more text.

which you can then take away and apply the concepts from it to your real code.

Ed Morton
  • 35.9k
  • 6
  • 25
  • 60