How to read in a block
suntong opened this issue · 2 comments
suntong commented
The Example (15b)
https://godoc.org/github.com/spakin/awk#ex-package--15b
shows how to deal with lines between occurrences of the strings "start" and "stop" (AWK: /start/, /stop/
).
My question is, how to define the awk.Script
func so that it will process the lines between "start" and "stop" as a block? -- My "start" and "stop" case is "<table>
" and "</table>
", and I got the error of invalid XML format
, and I believe I'm reading them one line at a time, instead of all the lines between "<table>
" and "</table>
" as a block.
Please help. Thanks.
spakin commented
One possibility is to buffer all the lines between <table>
and </table>
and process the buffer later on, as in the following example:
package main
import (
"fmt"
"github.com/spakin/awk"
"os"
)
func main() {
s := awk.NewScript()
s.State = make([]byte, 0)
s.AppendStmt(awk.Auto("<table>", "</table>"), func(s *awk.Script) {
str := []byte(s.F(0).String() + "\n")
s.State = append(s.State.([]byte), str...)
if s.F(0).Match("</table>") {
s.Println("=== I HAVE A TABLE ===")
fmt.Print(string(s.State.([]byte)))
s.Println("=== ALL DONE ===")
s.State = make([]byte, 0)
}
})
s.Run(os.Stdin)
}
— Scott
suntong commented
Thanks a lot Scott!