Skip to content

--no-split-newline option for string command #7227

Open
@mattdutson

Description

From the documentation of string:

STRING arguments are taken from the command line unless standard input is connected to a pipe or a file, in which case they are read from standard input, one STRING per line.

Here's an example of when newline splitting is a problem. Consider file.txt with contents 1\n2. If we run cat file.txt | string replace 1\n '', the output is 1\n2 and not the desired 2. This is because stdin is split on the newline and treated as two separate strings by string replace. The result is then re-joined with a newline when printed to stdout.

In this example, we could do string replace 1\n '' (cat file.txt | string collect), but isn't this unsafe for large file.txt? We could also do sed -z 's/1\n//' file.txt, but the -z option is not universal (macOS sed doesn't have it).

What I'm envisioning is a --no-split-newline option which would cause stdin to be treated as a single string. In the example we could do cat file.txt | string replace --no-split-newline 1\n '' and would get the output 2.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions