0

we want to relapse the [1023,1024,1022] as described in the following file , with [1022]

more file.exm

{"topic":"nj_hgf_dfgef","partition":0,"replicas":[1023,1024,1022]},
{"topic":"nj_hgf_dfgef","partition":1,"replicas":[1024,1022,1023]},
{"topic":"nj_hgf_dfgef","partition":2,"replicas":[1022,1023,1024]},

expected output

{"topic":"nj_hgf_dfgef","partition":0,"replicas":[1022]},
{"topic":"nj_hgf_dfgef","partition":1,"replicas":[1022]},
{"topic":"nj_hgf_dfgef","partition":2,"replicas":[1022]},

we try

 sed -i s'/[0-9],[0-9],[0-9]/[1022]/g'  file.exm

but without success

3 Answers 3

1

try

sed -e 's/[0-9]\{4\},[0-9]\{4\},[0-9]\{4\}/1022/'

where

  • \{4\} is for exactly 4 occurences.

https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html

1

Your regular expression is not correct, because you expect each time to have only 1 digit per number. Instead, try this:

sed -i s'/[0-9]+,[0-9]+,[0-9]+/1022/g'  file.exm

If the + does not work with your version of sed, replace it with a *:

sed -i s'/[0-9]*,[0-9]*,[0-9]*/1022/g'  file.exm

If you want to test your regular expressions, I suggest you to use one of the online sites like https://regex101.com/

2
  • This will add extra square brackets.
    – Toto
    Commented Jun 3, 2020 at 12:02
  • True, I copy-pasted too quickly. I edited my answer. Commented Jun 3, 2020 at 12:05
0

A simple awk solution:

$ awk  -F':' '{ print $1":"$2":"$3":[1022]}," }' file.exm
{"topic":"nj_hgf_dfgef","partition":0,"replicas":[1022]},
{"topic":"nj_hgf_dfgef","partition":1,"replicas":[1022]},
{"topic":"nj_hgf_dfgef","partition":2,"replicas":[1022]},
$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .