Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mlr cat --filename / --filenum #1080

Merged
merged 2 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codespellignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ numer
Wit
te
wee
RO
2 changes: 1 addition & 1 deletion docs/src/data-diving-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ bin_lo bin_hi flag_count u_count v_count
1.0900000000000003 1.1900000000000002 0 0 25
</pre>

Look at univariate stats by color and shape. In particular, color-dependent flag probabilities pop out, aligning with their original Bernoulli probablities from the data-generator script:
Look at univariate stats by color and shape. In particular, color-dependent flag probabilities pop out, aligning with their original Bernoulli probabilities from the data-generator script:

<pre class="pre-highlight-in-pair">
<b>mlr --opprint stats1 -a min,mean,max -f flag,u,v -g color \</b>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/data-diving-examples.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ GENMD-RUN-COMMAND
mlr --opprint histogram -f flag,u,v --lo -0.1 --hi 1.1 --nbins 12 data/colored-shapes.dkvp
GENMD-EOF

Look at univariate stats by color and shape. In particular, color-dependent flag probabilities pop out, aligning with their original Bernoulli probablities from the data-generator script:
Look at univariate stats by color and shape. In particular, color-dependent flag probabilities pop out, aligning with their original Bernoulli probabilities from the data-generator script:

GENMD-RUN-COMMAND
mlr --opprint stats1 -a min,mean,max -f flag,u,v -g color \
Expand Down
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: 1 addition & 1 deletion docs/src/new-in-miller-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ For `mlr put` and `mlr filter`, parse-error messages now include location inform

<pre class="pre-non-highlight-non-pair">
mlr: cannot parse DSL expression.
Parse error on token ">" at line 63 columnn 7.
Parse error on token ">" at line 63 column 7.
</pre>

### Scripting
Expand Down
2 changes: 1 addition & 1 deletion docs/src/new-in-miller-6.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ For `mlr put` and `mlr filter`, parse-error messages now include location inform

GENMD-CARDIFY
mlr: cannot parse DSL expression.
Parse error on token ">" at line 63 columnn 7.
Parse error on token ">" at line 63 column 7.
GENMD-EOF

### Scripting
Expand Down
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: 1 addition & 1 deletion docs/src/reference-dsl-output-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ JSON vs. tabular formats](flatten-unflatten.md) for more information.
The reason for this is part historical and part technical. As we'll see below,
you can do lots of syntactical things with `emit`, `emitp`, and `emitf`,
including printing them side-by-side, index them, redirect the output to files,
etc. What this means syntatically is that Miller's parser needs to handle all
etc. What this means syntactically is that Miller's parser needs to handle all
sorts of commas, parentheses, and so on:

<pre class="pre-non-highlight-non-pair">
Expand Down
2 changes: 1 addition & 1 deletion docs/src/reference-dsl-output-statements.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ JSON vs. tabular formats](flatten-unflatten.md) for more information.
The reason for this is part historical and part technical. As we'll see below,
you can do lots of syntactical things with `emit`, `emitp`, and `emitf`,
including printing them side-by-side, index them, redirect the output to files,
etc. What this means syntatically is that Miller's parser needs to handle all
etc. What this means syntactically is that Miller's parser needs to handle all
sorts of commas, parentheses, and so on:

GENMD-CARDIFY
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