-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Add parameter -Delimiter to Set-Content, Out-File, Out-String to complement Get-Content -Delimiter #3855
Description
Get-Content -Delimiter can be used to read text records with an arbitrary (literally matching) terminator into an array of strings, as an alternative to reading line by line.
To complement this functionality, the general-purpose text-outputting cmdlets should support the same parameter for creating such files:
Set-Content/Add-ContentOut-FileOut-String
(ConvertTo-Csv and Export-Csv already have a -Delimiter parameter, but there it is a field separator (line-internal), and a newline is implied as the record separator.)
Note:
-
-Delimiter ''would effectively behave the same way as-NoNewline(which is currently missing fromOut-String- see Out-String cmdlet should support -NoNewline too #3684). -
-Delimiter "`n"on Windows and-Delimiter "`r`n"on Unix would then allow creating text files with the respective other platform's newline sequence on demand - which would also cover Let us specify EOL when using out-file #2145. -
The term delimiter is problematic and already ambiguous in terms of what is currently implemented:
-
In the context of
*-Csv*cmdlets,-Delimiterrefers to a separator - something placed between elements (fields), but not after the last. -
In the context of
Get-Content,-Delimiterrefers to an (optional) terminator, something (expected to be placed after every element (record), including the last.
Get-Content -Delimitertreats input that differs only with respect to a trailing delimiter identically - to signal an empty trailing element two trailing delimiters are needed. -
This
Get-Content -Delimiterbehavior implies thatSet-Content,Out-File, ... should also treat-Delimiterto mean terminator, and add a trailing delimiter instance to the last record.
-
Desired behavior
'one', 'two' | Set-Content t.txt -Delimiter '@'
get-content t.txt
'---'
get-content t.txt -Delimiter '@' | % { "[$_]" }one@two@
---
[one]
[two]Environment Data
PowerShell Core v6.0.0-beta (v6.0.0-beta.1)