[英]How to convert a csv list (horizontal) from vertical name value pair set
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? 有什么建议吗?
Perl one-liner: Perl一线:
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... 这比尝试多遍正则表达式要容易得多,后者很可能会在您正确设置之前中断几次,并且要取决于要排序的数据,并且可能需要第二个正则表达式才能最后格式化所有内容。 ..
load into jEdit (or Notepad++, or some other editor that can search/replace via regex. 加载到jEdit(或Notepad ++,或其他可以通过正则表达式搜索/替换的编辑器)中。
Step 1 is to make sure that the delimiter is a tab. 步骤1是确保定界符是制表符。
Then, search for 然后,搜寻
^(.*)\t(.*)\n\1
and replace that with 并替换为
$1\t$2,
Repeat the find/replace all until no more matches are found. 重复查找/全部替换,直到找不到更多匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.