On Tue, Apr 21, 2026 at 4:52 PM Tim Vergenz <[email protected]> wrote:
>
> TL;DR: With custom --rpl flag (also pasted below), you can use {[jq_expr]} to
> insert the shell-quoted result of passing the input into jq with the given
> jq_expr.
:
> Here's the implementation (also linked here), which I add to my
> ~/.parallel/config so it's always available:
>
> --rpl '{[(.*?)]} uq(); $Global::use{"IPC::Open2"} ||= eval "use IPC::Open2;
> 1;"; my $jq = open2(my $jq_out, my $jq_in, "jq", "-Rrc", "[ try(fromjson) //
> . | $$1 | tostring ] | join(\" \")") or die "Could not open pipe to jq!";
> print $jq_in $_; close $jq_in; $_ = <$jq_out>; chomp $_; waitpid $jq, 0; $?
> && die'
>
>
> It's a bit of a mouthful. 🫠 (And IIRC I think I had to put it all on one line
> because parallel expects each config file argument on one line only.)
THANK YOU!
Since 20170322 I have been worried if I would be the only person using
dynamic replacement strings.
I just tested your code: I was somewhat worried about a deadlock when
you write and read from the same thread. But I realized that jq will
not print anything until it has received a full JSON object - no
matter how big.
So it seems pretty sound. GNU Parallel already uses open3, so I will
be using that instead.
I do not understand why you want uq(), so I removed that. Save this in
~/.parallel/json:
--rpl '{[(.*?)]} my $pid = ::open3(my $in, my $out, my $err, "jq",
"-Rrc", "[ try(fromjson) // . | $$1 | tostring ] | join(\" \")") or
die "Could not open pipe to jq!"; print $in $_; close $in; $_= <$out>;
chomp $_; waitpid $pid, 0; $? && die "jq failed".<$err>'
Then run:
echo '{"name":"a","type":"two spaces","version":3}' | parallel -Jjson
echo '{[.type]}'
> If properly cleaned up, would there be any interest & openness from the
> maintainers in adding jq-expression placeholder syntax (either {[...]} or in
> some other form) into parallel directly? Maybe under the --plus flag?
Overall I like the idea, and I can easily see something like that
being part of --plus.
However, I am not sure about {[...]}. {} was originally chosen because
people knew it from 'xargs'.
If possible I want to make the syntax be recognizable. Can we find
examples where jq expressions are embedded in "brackets"? And what
brackets did people choose?
Also it has to be brackets that are not found in jq expressions. This
example would break if we chose {[ ]} because the regexp does not
count matching parenthesis:
jq '{result: [.type]}'
would be:
{[{result: [.type]}]}
which will break.
Maybe: {%jq: ... %} It would have the added benefit that it might be
able to extend to other languages.
/Ole