开发者

How can I replace commas per newlines, but only if they are not between brackets?

I was wondering if you could help me with a replacement script im trying here...

I've got something like this:

aaaaaa,aaaaa,aaaaa,aaaa,aaaaa,aaa,aaa,aaa,aaaa,aaa
aaaa,aaaaa,aaaaa,aaaaaaa,aaa[1,2], aaaaaaaa[5,6], aaaa,aaaaaaa

and want to order the file to get something like:

aaaaaa,
aaa,
aaaaa,
aaaaa[1,2],
aaaaaaaa[5,6],
aaaaa,
aaaaaaaa,
aaaa

I've tried replacing , with lin开发者_StackOverflow社区e-breaks, but it separates the [n,n] blocks, too.

Also tried replacing ,[^0-9] with line-breaks, but I loose the first char after the comma.

I've been trying sed, but I'm not sure how this would look like.

I've added a sample of the file:

x0.fieldB [1,2] ,x5.fieldC ,x0.fieldX ,x0.fieldA
(x0.fieldE [1,5] = x2.fieldZ) ) AND (x0.fieldG [1,2] = x3.fieldT ) )

and should get something like this:

x0.fieldB [1,2] ,
x5.fieldC ,
x0.fieldX ,
x0.fieldA (x0.fieldE [1,5] = x2.fieldZ) ) AND (x0.fieldG [1,2] = x3.fieldT ) )


In order to not lose that digit, you need to capture it with (escaped) parenthesis, and then substitute it with \1 as follows:

sed 's/\([^0-9]\),/\1\n/g' yourfile


Quick and dirty fix: Replace , and <]>, with enters.


Here is one solution that works in your case:

sed -e 's;\([A-Za-z \t]\),;\1,\n;g' -e 's;],;],\n;g'

What it does is replaces comma after any character in ranges A-Z or a-z, or a whitespace. And then replaces all ], occurrences. Seems to do exactly what you want. I am not a regexp or sed expert and most likely this can be written much better, so don't blame me :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜