Skip to content

Commit

Permalink
mlr cat --filename / --filenum
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Aug 20, 2022
1 parent c61f27f commit 8b915f4
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 6 deletions.
4 changes: 3 additions & 1 deletion docs/src/manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ VERBS
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.

check
Expand Down Expand Up @@ -3280,5 +3282,5 @@ SEE ALSO



2022-08-14 MILLER(1)
2022-08-20 MILLER(1)
</pre>
4 changes: 3 additions & 1 deletion docs/src/manpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ VERBS
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.

check
Expand Down Expand Up @@ -3259,4 +3261,4 @@ SEE ALSO



2022-08-14 MILLER(1)
2022-08-20 MILLER(1)
2 changes: 2 additions & 0 deletions docs/src/online-help.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Options:
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.
</pre>

Expand Down
2 changes: 2 additions & 0 deletions docs/src/reference-verbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ Options:
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.
</pre>

Expand Down
48 changes: 47 additions & 1 deletion internal/pkg/transformers/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func transformerCatUsage(
fmt.Fprintf(o, "-n Prepend field \"n\" to each record with record-counter starting at 1.\n")
fmt.Fprintf(o, "-N {name} Prepend field {name} to each record with record-counter starting at 1.\n")
fmt.Fprintf(o, "-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c\n")
fmt.Fprintf(o, "--filename Prepend current filename to each record.\n")
fmt.Fprintf(o, "--filenum Prepend current filenum (1-up) to each record.\n")
fmt.Fprintf(o, "-h|--help Show this message.\n")
}

Expand All @@ -50,6 +52,8 @@ func transformerCatParseCLI(
doCounters := false
counterFieldName := ""
var groupByFieldNames []string = nil
doFileName := false
doFileNum := false

for argi < argc /* variable increment: 1 or 2 depending on flag */ {
opt := args[argi]
Expand All @@ -74,6 +78,12 @@ func transformerCatParseCLI(
} else if opt == "-g" {
groupByFieldNames = cli.VerbGetStringArrayArgOrDie(verb, opt, args, &argi, argc)

} else if opt == "--filename" {
doFileName = true

} else if opt == "--filenum" {
doFileNum = true

} else {
transformerCatUsage(os.Stderr)
os.Exit(1)
Expand All @@ -89,6 +99,8 @@ func transformerCatParseCLI(
doCounters,
counterFieldName,
groupByFieldNames,
doFileName,
doFileNum,
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -107,6 +119,9 @@ type TransformerCat struct {
countsByGroup map[string]int64
counterFieldName string

doFileName bool
doFileNum bool

recordTransformerFunc RecordTransformerFunc
}

Expand All @@ -115,6 +130,8 @@ func NewTransformerCat(
doCounters bool,
counterFieldName string,
groupByFieldNames []string,
doFileName bool,
doFileNum bool,
) (*TransformerCat, error) {

if counterFieldName != "" {
Expand All @@ -127,6 +144,8 @@ func NewTransformerCat(
counter: 0,
countsByGroup: make(map[string]int64),
counterFieldName: counterFieldName,
doFileName: doFileName,
doFileNum: doFileNum,
}

if !doCounters {
Expand All @@ -151,7 +170,12 @@ func (tr *TransformerCat) Transform(
outputDownstreamDoneChannel chan<- bool,
) {
HandleDefaultDownstreamDone(inputDownstreamDoneChannel, outputDownstreamDoneChannel)
tr.recordTransformerFunc(inrecAndContext, outputRecordsAndContexts, inputDownstreamDoneChannel, outputDownstreamDoneChannel)
tr.recordTransformerFunc(
inrecAndContext,
outputRecordsAndContexts,
inputDownstreamDoneChannel,
outputDownstreamDoneChannel,
)
}

// ----------------------------------------------------------------
Expand All @@ -161,6 +185,14 @@ func (tr *TransformerCat) simpleCat(
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
if !inrecAndContext.EndOfStream {
if tr.doFileName {
inrecAndContext.Record.PrependCopy("filename", mlrval.FromString(inrecAndContext.Context.FILENAME))
}
if tr.doFileNum {
inrecAndContext.Record.PrependCopy("filenum", mlrval.FromInt(inrecAndContext.Context.FILENUM))
}
}
outputRecordsAndContexts.PushBack(inrecAndContext)
}

Expand All @@ -176,6 +208,13 @@ func (tr *TransformerCat) countersUngrouped(
tr.counter++
key := tr.counterFieldName
inrec.PrependCopy(key, mlrval.FromInt(tr.counter))

if tr.doFileName {
inrec.PrependCopy("filename", mlrval.FromString(inrecAndContext.Context.FILENAME))
}
if tr.doFileNum {
inrec.PrependCopy("filenum", mlrval.FromInt(inrecAndContext.Context.FILENUM))
}
}
outputRecordsAndContexts.PushBack(inrecAndContext)
}
Expand Down Expand Up @@ -208,6 +247,13 @@ func (tr *TransformerCat) countersGrouped(

key := tr.counterFieldName
inrec.PrependCopy(key, mlrval.FromInt(counter))

if tr.doFileName {
inrec.PrependCopy("filename", mlrval.FromString(inrecAndContext.Context.FILENAME))
}
if tr.doFileNum {
inrec.PrependCopy("filenum", mlrval.FromInt(inrecAndContext.Context.FILENUM))
}
}
outputRecordsAndContexts.PushBack(inrecAndContext)
}
4 changes: 3 additions & 1 deletion man/manpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ VERBS
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.

check
Expand Down Expand Up @@ -3259,4 +3261,4 @@ SEE ALSO



2022-08-14 MILLER(1)
2022-08-20 MILLER(1)
6 changes: 4 additions & 2 deletions man/mlr.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: mlr
.\" Author: [see the "AUTHOR" section]
.\" Generator: ./mkman.rb
.\" Date: 2022-08-14
.\" Date: 2022-08-20
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
.TH "MILLER" "1" "2022-08-14" "\ \&" "\ \&"
.TH "MILLER" "1" "2022-08-20" "\ \&" "\ \&"
.\" -----------------------------------------------------------------
.\" * Portability definitions
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1071,6 +1071,8 @@ Options:
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.
.fi
.if n \{\
Expand Down
2 changes: 2 additions & 0 deletions test/cases/cli-help/0001/expout
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Options:
-n Prepend field "n" to each record with record-counter starting at 1.
-N {name} Prepend field {name} to each record with record-counter starting at 1.
-g {a,b,c} Optional group-by-field names for counters, e.g. a,b,c
--filename Prepend current filename to each record.
--filenum Prepend current filenum (1-up) to each record.
-h|--help Show this message.

================================================================
Expand Down
1 change: 1 addition & 0 deletions test/cases/verb-cat/0015/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr cat --filename --filenum test/input/abixy
Empty file added test/cases/verb-cat/0015/experr
Empty file.
10 changes: 10 additions & 0 deletions test/cases/verb-cat/0015/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
filenum=1,filename=test/input/abixy,a=pan,b=pan,i=1,x=0.34679014,y=0.72680286
filenum=1,filename=test/input/abixy,a=eks,b=pan,i=2,x=0.75867996,y=0.52215111
filenum=1,filename=test/input/abixy,a=wye,b=wye,i=3,x=0.20460331,y=0.33831853
filenum=1,filename=test/input/abixy,a=eks,b=wye,i=4,x=0.38139939,y=0.13418874
filenum=1,filename=test/input/abixy,a=wye,b=pan,i=5,x=0.57328892,y=0.86362447
filenum=1,filename=test/input/abixy,a=zee,b=pan,i=6,x=0.52712616,y=0.49322129
filenum=1,filename=test/input/abixy,a=eks,b=zee,i=7,x=0.61178406,y=0.18788492
filenum=1,filename=test/input/abixy,a=zee,b=wye,i=8,x=0.59855401,y=0.97618139
filenum=1,filename=test/input/abixy,a=hat,b=wye,i=9,x=0.03144188,y=0.74955076
filenum=1,filename=test/input/abixy,a=pan,b=wye,i=10,x=0.50262601,y=0.95261836

0 comments on commit 8b915f4

Please sign in to comment.