Skip to content

Commit 991930f

Browse files
authored
add filename support for seqkit replace (#488)
1 parent f9bc0c2 commit 991930f

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

seqkit/cmd/replace.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ Filtering records to edit:
132132
replaceWithNR = true
133133
}
134134

135+
var replaceWithFN bool
136+
if reFN.Match(replacement) {
137+
replaceWithFN = true
138+
}
139+
135140
var replaceWithKV bool
136141
var kvs map[string]string
137142
if reKV.Match(replacement) {
@@ -429,6 +434,10 @@ Filtering records to edit:
429434
r = reNR.ReplaceAll(r, []byte(fmt.Sprintf(nrFormat, nr)))
430435
}
431436

437+
if replaceWithFN {
438+
r = reFN.ReplaceAll(r, []byte(file))
439+
}
440+
432441
if replaceWithKV {
433442
founds = patternRegexp.FindAllSubmatch(record.Name, -1)
434443
if len(founds) > 1 {
@@ -483,7 +492,7 @@ func init() {
483492
"replacement. supporting capture variables. "+
484493
" e.g. $1 represents the text of the first submatch. "+
485494
"ATTENTION: for *nix OS, use SINGLE quote NOT double quotes or "+
486-
`use the \ escape character. Record number is also supported by "{nr}".`+
495+
`use the \ escape character. Record number and file name is also supported by "{nr}" and "{fn}".`+
487496
`use ${1} instead of $1 when {kv} given!`)
488497
replaceCmd.Flags().IntP("nr-width", "", 1, `minimum width for {nr} in flag -r/--replacement. e.g., formatting "1" to "001" by --nr-width 3`)
489498
// replaceCmd.Flags().BoolP("by-name", "n", false, "replace full name instead of just id")
@@ -508,3 +517,4 @@ func init() {
508517

509518
var reNR = regexp.MustCompile(`\{(NR|nr)\}`)
510519
var reKV = regexp.MustCompile(`\{(KV|kv)\}`)
520+
var reFN = regexp.MustCompile(`\{(FN|fn)\}`)

0 commit comments

Comments
 (0)