I have a large file with many instances of variable length lists of numbers in square brackets, max one list per line, list is never empty, e.g.:
[1, 45, 54, 78] or [32]
I want to get rid of the square brackets and the commas, e.g.:
1 45 54 78 or 32
I can successfully match them with this regex in sed:
\\[\\([0-9]*\\)\\(, \\([0-9]*\\)\\)*\\]
but I don't know how to use group numbers to refer to the groups I want, e.g. doing:
sed 's/\\t\[\\([0-9]*\\)\\(, \\([0-9]*\\)\\)*\\]/\\t\\1 \\3/g'
will only result in the destination file getting the first and the last numbers in the list.
(I did solve my problem using awk, but am wondering if it can be done using sed)
Is there any way to refer to variable number of groups in sed?
This might work for you (GNU sed):
This finds the pattern, marks it with a newline either side and copies the entire line to the hold space. It then deletes the brackets and commas in the pattern and recombines the altered with the original pattern and then repeats until no further patterns are found.