Respect OFS/OPS in mlr grep

This commit is contained in:
John Kerl 2023-06-05 23:21:42 -04:00
parent 4050f566fa
commit 0217dba5ab
2 changed files with 17 additions and 9 deletions

View file

@ -12,28 +12,28 @@ func (mlrmap *Mlrmap) Print() {
os.Stdout.WriteString("\n")
}
func (mlrmap *Mlrmap) Fprint(file *os.File) {
(*file).WriteString(mlrmap.ToDKVPString())
(*file).WriteString(mlrmap.ToDKVPString(",", "="))
}
func (mlrmap *Mlrmap) ToDKVPString() string {
func (mlrmap *Mlrmap) ToDKVPString(ofs, ops string) string {
var buffer bytes.Buffer // stdio is non-buffered in Go, so buffer for ~5x speed increase
for pe := mlrmap.Head; pe != nil; pe = pe.Next {
buffer.WriteString(pe.Key)
buffer.WriteString("=")
buffer.WriteString(ops)
buffer.WriteString(pe.Value.String())
if pe.Next != nil {
buffer.WriteString(",")
buffer.WriteString(ofs)
}
}
return buffer.String()
}
func (mlrmap *Mlrmap) ToNIDXString() string {
func (mlrmap *Mlrmap) ToNIDXString(ofs string) string {
var buffer bytes.Buffer // stdio is non-buffered in Go, so buffer for ~5x speed increase
for pe := mlrmap.Head; pe != nil; pe = pe.Next {
buffer.WriteString(pe.Value.String())
if pe.Next != nil {
buffer.WriteString(",")
buffer.WriteString(ofs)
}
}
return buffer.String()

View file

@ -51,7 +51,7 @@ func transformerGrepParseCLI(
pargi *int,
argc int,
args []string,
_ *cli.TOptions,
options *cli.TOptions,
doConstruct bool, // false for first pass of CLI-parse, true for second pass
) IRecordTransformer {
@ -122,6 +122,8 @@ func transformerGrepParseCLI(
regexp,
invert,
valuesOnly,
options.WriterOptions.OFS,
options.WriterOptions.OPS,
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
@ -136,17 +138,23 @@ type TransformerGrep struct {
regexp *regexp.Regexp
invert bool
valuesOnly bool
ofs string
ops string
}
func NewTransformerGrep(
regexp *regexp.Regexp,
invert bool,
valuesOnly bool,
ofs string,
ops string,
) (*TransformerGrep, error) {
tr := &TransformerGrep{
regexp: regexp,
invert: invert,
valuesOnly: valuesOnly,
ofs: ofs,
ops: ops,
}
return tr, nil
}
@ -164,9 +172,9 @@ func (tr *TransformerGrep) Transform(
inrec := inrecAndContext.Record
var inrecAsString string
if tr.valuesOnly {
inrecAsString = inrec.ToNIDXString()
inrecAsString = inrec.ToNIDXString(tr.ofs)
} else {
inrecAsString = inrec.ToDKVPString()
inrecAsString = inrec.ToDKVPString(tr.ofs, tr.ops)
}
matches := tr.regexp.Match([]byte(inrecAsString))
if tr.invert {