简体   繁体   English

用纯sed复杂地转置柱子

[英]complex transposing of columns with pure sed

I tried a couple of hours to find a pure solution for this question. 我试了几个小时为这个问题找到一个纯粹的解决方案。 Obviously, unfortunately I had not succeed. 显然,遗憾的是我没有成功。 A really tricky question. 一个非常棘手的问题。


Examples (from the awk question): 例子(来自awk问题):

  • input: 输入:
aaa 111    
aaa 222
aaa 333
bbb 444
bbb 555
ccc 666
  • output: 输出:
aaa 111,222,333
bbb 444,555
ccc 666

  • input 输入
APM00065101435 189
APM00065101435 190
APM00065101435 191
APM00065101435 390
190104555 00C7
190104555 00D1
190104555 00E1
190104555 0454
190104555 0462
APM00065101435 391
APM00065101435 392
  • output 产量
APM00065101435 189,190,191,390
190104555 00C7,00D1,00E1,0454,0462
APM00065101435 391,392

What have I tried? 我试过了什么? Some of my non working examples: 我的一些非工作示例:

sed -nr '1{h;b};H;x;/(\S+).*\n\1.*\'/M{x;b};s/.*\'//m;s/\n\S*\s*/,/g;s/,$//;p' file
sed -nr '1{h;b};H;x;h;s/(\S+).*\n(\S+).*\'/\1\n\2/m;/(\S+)\n\1\'/M{$!b;g;bk};g;s/\n.*\'//m;:k;s/^\S+\s//2mg;s/\n/,/g;p;x;s/.*\n//;h;$l' file2
sed -nr 'H;g;s/(\S+)\s.*/\1/gm;/(\S+)\n\1\'/M{$!b;g;bk};g;1d;s/\n.*\'//m;:k;s/\n\S+\s/,/2g;s/\n//;p;g;s/\n.*(\n.*)$/\1/;h' file2

Thanks for reading this. 感谢您阅读本文。

This might work for you (GNU sed): 这可能适合你(GNU sed):

sed -r ':a;$!N;s/^(([^ ]+ ).*)\n\2/\1,/;ta;P;D' file

or if you prefer: 或者如果您愿意:

sed -r ':a;$!N;s/^((\S+\s).*)\n\2/\1,/;ta;P;D' file

This reads 2 lines into the pattern space, compares the beginning of each line and if they are the same replaces the beginning of the second line that matches the first with a comma and repeats. 这将在模式空间中读取2行,比较每行的开头,如果它们相同,则用逗号替换第一行匹配第一行的开头并重复。 If the lines do not match it prints out the first line. 如果线条不匹配则打印出第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM