如何替换每个换行符的逗号,但前提是它们不在括号之间?
我想知道您是否可以帮助我使用我在这里尝试的替换脚本...
我有这样的内容:
aaaaaa,aaaaa,aaaaa,aaaa,aaaaa,aaa,aaa,aaa,aaaa,aaa
aaaa,aaaaa,aaaaa,aaaaaaa,aaa[1,2], aaaaaaaa[5,6], aaaa,aaaaaaa
并且想要订购该文件以获取类似的内容:
aaaaaa,
aaa,
aaaaa,
aaaaa[1,2],
aaaaaaaa[5,6],
aaaaa,
aaaaaaaa,
aaaa
我尝试替换 ,
带有换行符,但它也分隔 [n,n]
块。
还尝试用换行符替换 ,[^0-9]
,但我丢失了逗号后的第一个字符。
我一直在尝试 sed,但我不确定这会是什么样子。
我添加了文件的示例:
x0.fieldB [1,2] ,x5.fieldC ,x0.fieldX ,x0.fieldA
(x0.fieldE [1,5] = x2.fieldZ) ) AND (x0.fieldG [1,2] = x3.fieldT ) )
并且应该得到如下内容:
x0.fieldB [1,2] ,
x5.fieldC ,
x0.fieldX ,
x0.fieldA (x0.fieldE [1,5] = x2.fieldZ) ) AND (x0.fieldG [1,2] = x3.fieldT ) )
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 line-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 ) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了不丢失该数字,您需要使用(转义的)括号捕获它,然后用
\1
替换它,如下所示:In order to not lose that digit, you need to capture it with (escaped) parenthesis, and then substitute it with
\1
as follows:快速但肮脏的修复:将 , 和 <]> 替换为 Enter。
Quick and dirty fix: Replace , and <]>, with enters.
这是一种适合您情况的解决方案:
它的作用是替换 AZ 或 az 范围内的任何字符或空格后的逗号。然后替换所有出现的
],
。似乎完全按照你的意愿去做。我不是regexp
或sed
专家,很可能这可以写得更好,所以不要责怪我:-)Here is one solution that works in your case:
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 aregexp
orsed
expert and most likely this can be written much better, so don't blame me :-)