如何从垂直名称值对集转换 csv 列表(水平)

发布于 2024-08-21 09:28:36 字数 211 浏览 3 评论 0原文

我有一个这样的列表:

GTPYANJ         695848
GTPYANJ         27811
FPORTAL3        432532

我想使用正则表达式将其变成这样:

GTPYANJ,695848,27811
FPORTAL3,432532

建议?

I have a list like this:

GTPYANJ         695848
GTPYANJ         27811
FPORTAL3        432532

I want to turn it into this using regular expressions:

GTPYANJ,695848,27811
FPORTAL3,432532

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

泡沫很甜 2024-08-28 09:28:36

加载到 jEdit(或 Notepad++,或其他可以通过正则表达式搜索/替换的编辑器)中。

步骤 1 是确保分隔符是制表符。

然后,搜索

^(.*)\t(.*)\n\1

并替换为

$1\t$2,

重复查找/替换所有,直到不再匹配被发现。

load into jEdit (or Notepad++, or some other editor that can search/replace via regex.

Step 1 is to make sure that the delimiter is a tab.

Then, search for

^(.*)\t(.*)\n\1

and replace that with

$1\t$2,

Repeat the find/replace all until no more matches are found.

狠疯拽 2024-08-28 09:28:36

Perl 单行代码:

perl -e 'while(<>) { chomp; ($tag, $num) = split /\s+/; $tmp{$tag} .= ",$num"; } foreach $t (sort keys %tmp) { print $t.$tmp{$t}."\n" } '  myfile.txt

比尝试将多遍正则表达式拼凑在一起要容易得多,在正确之前,它很可能会中断几次,并且这取决于要排序的数据,并且可能需要第二个正则表达式来重新格式化一切都在最后……

Perl one-liner:

perl -e 'while(<>) { chomp; ($tag, $num) = split /\s+/; $tmp{$tag} .= ",$num"; } foreach $t (sort keys %tmp) { print $t.$tmp{$t}."\n" } '  myfile.txt

Much easier than trying to hobble together a multi-pass regex that will most likely break a couple of times before you get it right, and which depends on the data being sorted, and which might require a second regex to reformat everything at the end...

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