Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed command returns error about Unmatched (

I need to get rid of the double square brackets, I only need them once. However I get the error below after this command:

sed -ri 's/("opening_hours": \[\[)(.*?)(\]\]/"opening_hours": \[\2\]/' ./foo.txt

Error: sed: -e expression #1, char 60: Unmatched ( or (

input(foo.txt):

"opening_hours": [["Pendant les expositions : le jeudi de 12h \u00e0 20h et\u00a0du vendredi au dimanche, de 12h \u00e0 18h"]]
"opening_hours": [["\u00d6ffnungszeiten: Fr-Sa, 15-7 Uhr und nach Vereinbarung", "Open hours: Fri-Sat, 3-7pm and by appointment"]]

expected output:

"opening_hours": ["Pendant les expositions : le jeudi de 12h \u00e0 20h et\u00a0du vendredi au dimanche, de 12h \u00e0 18h"]
"opening_hours": ["\u00d6ffnungszeiten: Fr-Sa, 15-7 Uhr und nach Vereinbarung", "Open hours: Fri-Sat, 3-7pm and by appointment"]
like image 476
jjk Avatar asked Sep 13 '26 20:09

jjk


2 Answers

For better clarity, I suggest using two s commands:

sed 's/\[//; s/]//' foo.txt
like image 120
Cyrus Avatar answered Sep 16 '26 13:09

Cyrus


For matching the exact brackets with your shown samples I would suggest to use following. Where I am using 2 substitutions but regex is to match more accurate brackets here.

sed 's/": \[\["/": [":/; s/"\]\]$/"]/'  Input_file
like image 39
RavinderSingh13 Avatar answered Sep 16 '26 13:09

RavinderSingh13