String interpolation
11xx opened this issue · comments
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
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!
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
@nitsanavni, well, in that case you could just do something like "{ a: " + a + ", b: " + b + " }"
. :)
@pkoppstein, couldn't you just write '"a1bc2" | gsub("(?<x>[a-z])[0-9]"; .x|ascii_upcase)'
? Are there cases where this does not work?
Yes. Not having string interpolation is a source of such frustration that I wasn’t thinking clearly. My apologies.
Yes. Not having string interpolation is a source of such frustration that I wasn’t thinking clearly. My apologies.
No worries. :)
This is now implemented in #109.