Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove a substring from a string

Tags:

jq

I want to remove , from a string in jq. Take the following example, how to remove , when outputting 1,2?

$ jq -r .x <<< '{"x":"1,2"}'
1,2
like image 298
user1424739 Avatar asked Aug 04 '26 01:08

user1424739


2 Answers

You can use sub.

Filter

.x | sub(","; " ")

Input

{"x":"1,2"}

Output

1 2

Demo

https://jqplay.org/s/IaViogZTsI

like image 89
Logan Lee Avatar answered Aug 05 '26 19:08

Logan Lee


You didn't make clear what output you wanted. A literal reading suggests you want 12, but I find it more likely that you want each of the comma-separated items to be output on separate lines. The following achieves this:

jq -r '.x | split( "," )[]'

For the provided input, this outputs

1
2

Demo on jqplay

But if 12 is indeed what's desired, adding a join will do the trick.

jq -r '.x | split( "," ) | join( "" )'
12

Demo on jqplay

like image 41
ikegami Avatar answered Aug 05 '26 20:08

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!