如何替换每个换行符的逗号,但前提是它们不在括号之间?

发布于 2024-12-02 15:16:47 字数 795 浏览 0 评论 0原文

我想知道您是否可以帮助我使用我在这里尝试的替换脚本...

我有这样的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-12-09 15:16:47

为了不丢失该数字,您需要使用(转义的)括号捕获它,然后用 \1 替换它,如下所示:

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

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
那片花海 2024-12-09 15:16:47

快速但肮脏的修复:将 , 和 <]> 替换为 Enter。

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

放赐 2024-12-09 15:16:47

这是一种适合您情况的解决方案:

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

它的作用是替换 AZ 或 az 范围内的任何字符或空格后的逗号。然后替换所有出现的 ], 。似乎完全按照你的意愿去做。我不是 regexpsed 专家,很可能这可以写得更好,所以不要责怪我:-)

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文