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

String interpolation #75

Closed
11xx opened this issue Apr 27, 2023 · 7 comments
Closed

String interpolation #75

11xx opened this issue Apr 27, 2023 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@11xx
Copy link

11xx commented Apr 27, 2023

It would be great to print values with arbitraty text like jq's:

❯ echo '{"key1":"value1","key2":"value2"}' | jq '"The first key contains \(.key1), and the second, \"\(.key2)\""'
"The first key contains value1, and the second, \"value2\""

❯ echo '{"key1":"value1","key2":"value2"}' | jq -r '"The first key contains \(.key1), and the second, \"\(.key2)\""'
The first key contains value1, and the second, "value2"

Source: stedolan/jq#614

@11xx 11xx changed the title [FQ] Built-in printf-like function [FR] Built-in printf-like function Apr 27, 2023
@01mf02 01mf02 added enhancement New feature or request help wanted Extra attention is needed labels Jun 26, 2023
@01mf02 01mf02 changed the title [FR] Built-in printf-like function String interpolation Jun 26, 2023
@01mf02
Copy link
Owner

01mf02 commented Jun 26, 2023

I agree with you that this would be nice to have. The name of this functionality is called "string interpolation", and I have put it into the README among the jq functionality that jaq does not implement yet. However, I do not have the motivation to work on this myself. If you feel like implementing it, go ahead!

@nitsanavni
Copy link

Here's how I do it currently. Not as readable as I wished.

def interpolate: map(tostring) | add;
def s: if isarray then interpolate else tostring end;

def a: "a";
def b: "b";

# usage
["{ a: ", a, ", b: ", b, " }"] | s

@01mf02
Copy link
Owner

01mf02 commented Jun 26, 2023

@nitsanavni, well, in that case you could just do something like "{ a: " + a + ", b: " + b + " }". :)

@01mf02
Copy link
Owner

01mf02 commented Jul 17, 2023

@pkoppstein, couldn't you just write '"a1bc2" | gsub("(?<x>[a-z])[0-9]"; .x|ascii_upcase)'? Are there cases where this does not work?

@pkoppstein
Copy link

Yes. Not having string interpolation is a source of such frustration that I wasn’t thinking clearly. My apologies.

@01mf02
Copy link
Owner

01mf02 commented Jul 17, 2023

Yes. Not having string interpolation is a source of such frustration that I wasn’t thinking clearly. My apologies.

No worries. :)

@01mf02
Copy link
Owner

01mf02 commented Aug 29, 2023

This is now implemented in #109.

@01mf02 01mf02 closed this as completed Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants