Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using backreference as replacement string in JQ

Tags:

regex

jq

Assuming I want to extract the time part of a date/time string on the shell I can do something like:

echo "2021-06-10T10:42:30.016+0200" | sed 's|.*T\(.*\)\..*|\1|g'

This returns

10:42:30

How can I achieve the same with jq and sub()? I'm missing the right syntax. I was not able to find any good examples. E.g. I've been trying:

jq '<myid> | sub(".*T(.*)\\..*"; "\1")'
jq '<myid> | sub(".*T(.*)\\..*"; "$1")'
jq '<myid> | sub(".*T\\(.*\\)\\..*"; "\1")'

But none of this returns back what I want?

like image 740
Peter Meier Avatar asked Sep 19 '25 12:09

Peter Meier


2 Answers

In JQ we use named capture groups for that.

sub(".*T(?<x>.*)\\..*"; .x)
like image 184
oguz ismail Avatar answered Sep 21 '25 03:09

oguz ismail


In this case, you may remove all up to T and all the text starting with (and including) a dot with .*T|\.[^.]*$ regex:

s='{"node":"2021-06-10T10:42:30.016+0200"}'
jq '.node |= gsub(".*T|\\.[^.]*$"; "")' <<< "$s"

Output:

{
  "node": "10:42:30"
}

Pattern details:

  • .*T - any text and then T
  • | - or
  • \. - a dot
  • [^.]* - zero or more chars other than a .
  • $ - end of string.
like image 32
Wiktor Stribiżew Avatar answered Sep 21 '25 01:09

Wiktor Stribiżew