This can be done with a single sed
command (Tested in GNU sed 4.8. Assumes the whole expression is in a single line and there is no embedded single quote between a pair of matching single quotes):
echo "db={1:['a','b','c','d'], 2:['aa','bb','cc','dd']}" |
sed -E "s/^[^{]*\{//; s/\}[^}]*$//; s/([^:]+):\['([^']*)','([^']*)','([^']*)','([^']*)'\](, *)?/one\1='\2'\ntwo\1='\3'\nthree\1='\4'\nfour\1='\5'\n\n/g"
outputs
one1='a'
two1='b'
three1='c'
four1='d'
one2='aa'
two2='bb'
three2='cc'
four2='dd'
Explanation:
-E
Use extended regular expression so that we don't quote (
, )
, +
characters.
s/^[^{]*\{//;
Deletes characters at the beginning of the line until and including the {
character
s/\}[^}]*$//;
Deletes the }
character and trailing characters (if any) at the end of line
s/([^:]+):\['([^']*)','([^']*)','([^']*)','([^']*)'\](, *)?/one\1='\2'\ntwo\1='\3'\nthree\1='\4'\nfour\1='\5'\n\n/g
------- ------- ------- ------- ------- ----- -----------------------------------------------------
1 2 3 4 5 6 R
1: Captures the text until :
2: Captures the text between the first pair of single quotes
3: Captures the text between the second pair of single quotes
4: Captures the text between the third pair of single quotes
5: Captures the text between the fourth pair of single quotes
6: Captures the ,
and any number of trailing space characters. This subexpression is not used in the replacement text. ?
means this is optional.
R: Replacement text. \1
, \2
, \3
, \4
, and \5
are replaced with the corresponding captured text.
The g
flag at the end of the s
command ensures that the replacement is applied to all matches.