Giter VIP home page Giter VIP logo

Comments (7)

nicktimko avatar nicktimko commented on July 2, 2024

The program is pretty-printing the equivalent string with the newline. See https://yaml-multiline.info/ for how the multiline marks work, but the actual value is identical.

$ echo '{"string": "x\\ny"}' | yq -P
string: |-
  x
  y


$ echo '{"string": "x\\ny"}' | yq -P | yq -ojson
{
  "string": "x\ny"
}

If you have jq output the raw string, you see it actually includes a newline (0x0A)

$ echo '{"string": "x\\ny"}' | jq -r .string | xxd
00000000: 780a 790a                                x.y.

If you want an actual \n in the string and not a newline, it seems like a bug that yq won't accept the backslash escape (which would eat up the backslashes for the newline escape):

$ # yq ignores \\ and instead translates \n into 0x0A, so the true output has a backslash + newline
$ echo '{"string": "x"}' | yq '.string = "x\\ny"' | yq -r .string | xxd
00000000: 785c 0a79 0a                             x\.y.

$ # jq accepts \\n and puts literal '\n' in the output
$ echo '{"string": "x"}' | jq '.string = "x\\ny"' | jq -r .string | xxd
00000000: 785c 6e79 0a                             x\ny.

from yq.

nicktimko avatar nicktimko commented on July 2, 2024

Maybe related: #1692 #1814

from yq.

Skarlso avatar Skarlso commented on July 2, 2024

Bummer. Thanks for the info @nicktimko!

from yq.

nicktimko avatar nicktimko commented on July 2, 2024

@Skarlso did you intend to put the escaped string into the YAML (it contains the characters \ and n, not a newline)?

from yq.

Skarlso avatar Skarlso commented on July 2, 2024

Yes, that's an object. Something like

bla: value1
bla2: value2

Which is the replacement. That should be translated into a newline I believe?

If I would actually try and do a multi-line string I would get a weird |2 there.

yq -i '.test.this |= "    bla: bla
    twice: bla
"' test.yaml
➜  SAP cat test.yaml
test:
  this: |2
        bla: bla
        twice: bla

from yq.

Skarlso avatar Skarlso commented on July 2, 2024

Ah sorry, my bad, too many spaces. This is the equivalent.

yq -i '.test.this |= "bla: bla
twice: bla
"' test.yaml
➜  SAP cat test.yaml
test:
  this: |
    bla: bla
    twice: bla

Still got | in there...

from yq.

mikefarah avatar mikefarah commented on July 2, 2024

The | |- and other variations are how multi line strings are encoded in yaml. Unfortunately, the underlying yaml encoder does not give me many controls to be able to force particular variations; and sometimes it 'auto' formats things :(

It's a known issue against go-yaml; and there are a bunch of tickets against it - but no one has worked on that repo for many months now :(

from yq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.