Skip to content

Get-Content -Delimiter unexpectedly keeps the delimiter in the lines returned #3706

Closed

Description

Note: It may be too late to change this behavior, or perhaps it falls into Bucket 3: Unlikely Grey Area.
If the former, perhaps a -TrimDelimiter option could be added.

By default, Get-Content strips the newline character [sequence] from the lines it returns.

The generalization of this concept is to use the -Delimiter parameter, which allows specifying a custom line (record) delimiter (terminator / separator).

However, unlike with the default delimiter - a newline - whatever -Delimiter argument you specify is kept in the lines returned, which is:

  • an inconsistency

  • an inconvenience, because the delimiter - which by its nature is not part of the data itself - must be stripped explicitly before further processing.

Steps to reproduce

I'd expect the following 2 commands to be equivalent:

1,2 > t.txt; get-content                                     t.txt | % { "[$_]" }
1,2 > t.txt; get-content -delimiter ([environment]::newline) t.txt | % { "[$_]" }

Expected behavior

[1]
[2]
[1]
[2]

Actual behavior

[1]
[2]
[1
]
[2
]

Note how the trailing newlines were retained in the output from the 2nd command.

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on macOS 10.12.4

Activity

  1. MaximoTrinidad commented on May 5, 2017

    @MaximoTrinidad

    Hey Ilya (@iSazonov),

    Just wondering if this could be tied up with this other issue experiencing with Import-Csv:
    "Import-Csv does not handle newlines containing just \r (Carriage Return) #3692"

    Can someone confirmed this?

    :)

  2. mklement0 commented on May 5, 2017

    @mklement0
    ContributorAuthor

    Maximo Trinidad (@MaximoTrinidad): The other issue is somewhat related, though in the case of Import-Csv the proper fix is to have it accept \r-only line breaks too (in line with Get-Content), so I see less of a need for an explicit -Delimiter parameter there.

  3. MaximoTrinidad commented on May 5, 2017

    @MaximoTrinidad
  4. added
    WG-Engine-Providers
    Issue-Bug
    on May 5, 2017
  5. iSazonov commented on May 5, 2017

    @iSazonov
    Collaborator

    I agree that both should work uniformly Get-Content and Get-Content -Delimiter. Although the parameter name LineTerminator is more suitable.

    And it is an other issue - Import-Csv don't use FileProvider code. Although worth discussing that all file cmdlets (Out-File, Import-Csv, Export-Csv ...) have been based on FileProvider code to work uniformly.

    /cc jeffbi

  6. mklement0 commented on May 9, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    Historically, there's been a lot of confusion around the terms separator, delimiter, and terminator.

    I agree that -Delimiter is not a great name, and that terminator is more appropriate here - though, arguably, we're then not dealing with lines anymore, but with the abstraction of records (the terminology adopted by awk for instance, which has otherwise settled on separator, unfortunately).

    Therefore, bypassing the secondary issue of line vs. record, perhaps defining -Terminator as an alias for -Delimiter is appropriate (-Delimiter currently has no alias).

  7. iSazonov commented on May 10, 2017

    @iSazonov
    Collaborator
  8. mklement0 commented on May 11, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    In the context of CSV files, specifically, which are line-oriented, line terminator is the right term.

    By contrast, by allowing arbitrary terminator strings with -Delimiter, what a terminator terminates is no longer necessarily a line: with a terminator other than a newline (in any of its variations), any number of lines may then occur between instances of the terminator.

    In other words: the units of data may themselves be multi-line, so calling such a unit a line is inappropriate.

    That's why awk chose record, for instance, but my point is that it isn't necessary for the parameter name to reflect a term for the data unit, given that only one kind of terminator is supported (for the implied lines / data units).

    On a meta note: even though the label of your link suggests that it is to a specific comment on the issue page, the link is just to the issue page itself.
    A recent post by Jason Shirk (@lzybkr) exhibited the same problem.
    How are you generating these links? Is this a GitHub bug?

  9. iSazonov commented on May 11, 2017

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) You can copy such links from post header datetime string "mklement0 commented 9 minutes ago".
    It is GitHub bug.

  10. added
    Breaking-Change
    on Jun 1, 2017
  11. shreyjain362 commented on Jun 17, 2024

    @shreyjain362

    The issue is still not resolved. Why has it been marked "Resolution Fixed"? Michael Klement (@mklement0) Ilya (@iSazonov)

  12. mklement0 commented on Jun 17, 2024

    @mklement0
    ContributorAuthor

    Shrey Jain (@shreyjain362), it has been fixed in PowerShell 7 (which this repo is solely devoted to) - at least judging by running the original reproduction steps.

    Note that Windows PowerShell won't receive this fix, because it is no longer actively developed and only receives security-critical fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Breaking-ChangeIssue-BugResolution-FixedWG-Engine-Providers

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions