mirror of
https://github.com/johnkerl/miller.git
synced 2026-01-23 02:14:13 +00:00
Respect OFS/OPS in mlr grep
This commit is contained in:
parent
4050f566fa
commit
0217dba5ab
2 changed files with 17 additions and 9 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue