Skip to content

Rewriting

Rewriting

Edit-TrexText is the module’s trex rewrite and Protect-TrexText its trex redact. The examples are on rewriting and redaction.

Edit-TrexText

Replaces each match of a trex pattern with a rendered template or with what a script block returns for it.

With -Template, ${name} renders a register, ${0} the whole match, ${name:upper} a transform and ${ip:octet1-2} a typed slice. With -ScriptBlock, the block runs once per match with the Trex.Match as $_, and its last output replaces the match. Text piped in comes back rewritten; -Path writes each file’s rewritten text, -InPlace writes the files that have a match back where they are, and -Diff writes the unified diff each would take. -MaxCount replaces only the first matches of each input. A file named outright that holds a NUL byte is left as it is and named in a warning, unless -Binary asks for it; a walk passes over one.

-Head, -Tail and -Lines rewrite the first lines of each input, its last, or a range of them, counted in records of -Unit where it names one: the text written is that part alone, rewritten, and a file written back or described by its diff changes only there. -First and -Last are -Head and -Tail. -Follow writes a file’s rewritten text as it grows, after its tail, its open range or the whole of it, a line at a time, until the pipeline is stopped.

-Interactive puts each change to the person first, as its diff and the template of the tokens it replaces: they apply it, skip it, type another replacement, answer for every later change of the same template, apply everything after it, or quit, which leaves every file as it was. The changes accepted are written once the last has been answered, and how many a template answer skipped is said as a warning, with where under -ShowSkipped.

Alias: Edit-TxText

Edit-TrexText [-Pattern] <Object> [-Template] <string> -InputObject <string> [-MaxCount <uint>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Edit-TrexText [-Pattern] <Object> [-Template] <string> -Path <string[]> [-InPlace] [-Diff] [-Interactive] [-ShowSkipped] [-Context <uint>] [-Hidden] [-NoIgnore] [-Binary] [-MaxCount <uint>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Follow] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Edit-TrexText [-Pattern] <Object> [-Template] <string> -LiteralPath <string[]> [-InPlace] [-Diff] [-Interactive] [-ShowSkipped] [-Context <uint>] [-Hidden] [-NoIgnore] [-Binary] [-MaxCount <uint>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Follow] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Edit-TrexText [-Pattern] <Object> -ScriptBlock <scriptblock> -InputObject <string> [-MaxCount <uint>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Edit-TrexText [-Pattern] <Object> -ScriptBlock <scriptblock> -Path <string[]> [-InPlace] [-Diff] [-Interactive] [-ShowSkipped] [-Context <uint>] [-Hidden] [-NoIgnore] [-Binary] [-MaxCount <uint>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Follow] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Edit-TrexText [-Pattern] <Object> -ScriptBlock <scriptblock> -LiteralPath <string[]> [-InPlace] [-Diff] [-Interactive] [-ShowSkipped] [-Context <uint>] [-Hidden] [-NoIgnore] [-Binary] [-MaxCount <uint>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Follow] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
ParameterTypePipelineDescription
-BinaryswitchReads files that hold a NUL byte, which a walk treats as binary.
-ContextuintLines of context around each change in a diff; 3 when absent.
-DiffswitchWrites the unified diff each file would take instead of its text.
-FollowswitchWrites a file’s rewritten text as it grows, a line at a time, after its tail, its open range or the whole of it, until the pipeline is stopped.
-HeaduintRewrites the first this many lines of each input, or records of -Unit, reading a file no further.
-HiddenswitchReads hidden files and directories a walk would skip.
-InPlaceswitchWrites each file that has a match back where it is.
-InputObjectstringby valueThe text to rewrite; each string piped in is rewritten on its own.
-InteractiveswitchPuts each change to the person before it is written.
-LibraryTrex.LibraryThe atoms a source-text pattern is compiled against, in place of the session’s.
-LinesobjectRewrites a range of lines, or records of -Unit, counted from one: "100..200", "100..", "..200", "7", or PowerShell’s 100..200.
-LiteralPathstring[]by nameFiles or directories to rewrite, read as written, as Get-ChildItem pipes them.
-MaxCountuintReplaces only the first this many matches of each input.
-NoIgnoreswitchReads files an ignore rule excludes.
-Pathstring[]Files or directories to rewrite; wildcards expand.
-PatternobjectThe pattern: trex source text, or a Trex.Pattern.
-ScriptBlockscriptblockA script block run once per match, with the match as $_; its last output replaces the match.
-ShowSkippedswitchLists where each change a template answer skipped stands, beside the count -Interactive always says.
-TailuintRewrites the last this many lines of each input, or records of -Unit, reading a file backward from its end.
-TemplatestringThe template rendered in place of each match.
-UnitstringWhat -Head, -Tail and -Lines count: a line when absent, or a unit Find-TrexRecord reads, such as paragraph or block.

Writes string.

Protect-TrexText

Masks each match of a trex pattern, keeping the fields named.

Every character of a match becomes the mask, * when -Mask is not given, except the fields -Keep names, which stay where they stand: card:last4 keeps a card’s last four digits, ip:octet1-2 an address’s first two octets, e:domain a mail domain. -Mask takes one character, a token that stands in for each masked run, shape, which masks letters and digits and keeps every other character so the text still lexes as it did, or pseudonym, which gives each distinct value a stable name per kind. Text piped in comes back masked; -Path writes each file’s masked text, -InPlace writes the files back, and -Diff writes the unified diff. A file named outright that holds a NUL byte is left as it is and named in a warning, unless -Binary asks for it; a walk passes over one.

-Head, -Tail and -Lines mask the first lines of each input, its last, or a range of them, counted in records of -Unit where it names one: the text written is that part alone, masked, so nothing outside it is written unmasked, and a file written back or described by its diff changes only there. -First and -Last are -Head and -Tail. -Follow writes a file’s masked text as it grows, after its tail, its open range or the whole of it, a line at a time, until the pipeline is stopped.

Alias: Protect-TxText

Protect-TrexText [-Pattern] <Object> -InputObject <string> [-Keep <string[]>] [-Mask <string>] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Protect-TrexText [-Pattern] <Object> -Path <string[]> [-Keep <string[]>] [-Mask <string>] [-InPlace] [-Diff] [-Context <uint>] [-Hidden] [-NoIgnore] [-Binary] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Follow] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
Protect-TrexText [-Pattern] <Object> -LiteralPath <string[]> [-Keep <string[]>] [-Mask <string>] [-InPlace] [-Diff] [-Context <uint>] [-Hidden] [-NoIgnore] [-Binary] [-Head <uint>] [-Tail <uint>] [-Lines <Object>] [-Unit <string>] [-Follow] [-Library <Library>] [-WhatIf] [-Confirm] [<CommonParameters>]
ParameterTypePipelineDescription
-BinaryswitchReads files that hold a NUL byte, which a walk treats as binary.
-ContextuintLines of context around each change in a diff; 3 when absent.
-DiffswitchWrites the unified diff each file would take instead of its text.
-FollowswitchWrites a file’s masked text as it grows, a line at a time, after its tail, its open range or the whole of it, until the pipeline is stopped.
-HeaduintMasks the first this many lines of each input, or records of -Unit, reading a file no further.
-HiddenswitchReads hidden files and directories a walk would skip.
-InPlaceswitchWrites each file that has a match back where it is.
-InputObjectstringby valueThe text to mask; each string piped in is masked on its own.
-Keepstring[]The fields to leave in place, as a register and an accessor: card:last4, ip:octet1-2, e:domain.
-LibraryTrex.LibraryThe atoms a source-text pattern is compiled against, in place of the session’s.
-LinesobjectMasks a range of lines, or records of -Unit, counted from one: "100..200", "100..", "..200", "7", or PowerShell’s 100..200.
-LiteralPathstring[]by nameFiles or directories to mask, read as written, as Get-ChildItem pipes them.
-MaskstringWhat stands in for a masked character: one character, a token for each run, shape, or pseudonym; * when absent.
-NoIgnoreswitchReads files an ignore rule excludes.
-Pathstring[]Files or directories to mask; wildcards expand.
-PatternobjectThe pattern: trex source text, or a Trex.Pattern.
-TailuintMasks the last this many lines of each input, or records of -Unit, reading a file backward from its end.
-UnitstringWhat -Head, -Tail and -Lines count: a line when absent, or a unit Find-TrexRecord reads, such as paragraph or block.

Writes string.