Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split lines by '\n' from external file or external variable does not work #1430

Closed
Kopfbremse opened this issue Nov 13, 2022 · 2 comments
Closed
Labels

Comments

@Kopfbremse
Copy link

Version of yq: 4.30.2
Operating system: linux
Installed via: binary release

Describe the bug
Split lines from external file or external variable does not work.
This makes the processing of external line-delimited sources impossible in some cases.

Input file lines.txt

line 1
line 2

Commands

export LINES_FROM_VARIABLE='
line 1
line 2
'
export LINES_FROM_FILE='lines.txt'

echo --- 1#4: split lines ---
yq -n -o json '"line 1\nline 2" | split("\n")'                    # working

echo --- 2#4: split lines from internal variable ---
yq -n -o json '"lines 1\nlines 2" as $var | $var | split("\n")'   # working

echo --- 3#4: split lines from external variable ---
yq -n -o json 'env(LINES_FROM_VARIABLE) | split("\n")'            # fails
yq -n -o json 'strenv(LINES_FROM_VARIABLE) | split("\n")'         # fails

echo --- 4#4: split lines from file ---
yq -n -o json 'load_str(env(LINES_FROM_FILE)) | split("\n")'      # fails

Output

--- 1#4: split lines ---
[
  "line 1",
  "line 2"
]
--- 2#4: split lines from internal variable ---
[
  "lines 1",
  "lines 2"
]
--- 3#4: split lines from external variable ---
[
  "line 1 line 2"
]
[
  "\nline 1\nline 2\n"
]
--- 4#4: split lines from file ---
[
  "line 1\nline 2\n"
]

Expected behaviour
Output should be for each command

[
  "line 1",
  "line 2"
]
@Kopfbremse Kopfbremse changed the title Split lines from external file or external variable does not work Split lines by '\n' from external file or external variable does not work Nov 13, 2022
@mikefarah
Copy link
Owner

Nice find - will fix :)

@mikefarah
Copy link
Owner

Fixed in 4.30.3 - note that you will need to use strenv and not env (as env passes the content as-is to the yaml decoder, and that strips some of the newlines)

alexeagle pushed a commit to bazel-contrib/bazel-lib that referenced this issue Jun 23, 2023
Fixes d79f4d4
Fixes #463

This is required since versions of yq older than 4.30.3 do not parse "\n" correctly.
See mikefarah/yq#1430 and e02bb7194897edb916a6787fb8f8e5a7f432dd94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants