简体   繁体   English

如何将第三列合并为多列

[英]How to merge the 3rd column to multiple columns

How to merge the 3rd column to x, which starts off looking like this.如何将第 3 列合并到 x,它开始看起来像这样。

16 JUN  TRANSFER                                          blank          150       
        FROM ABC SOME COMPANY PTY LTD
18 JUN    VISA DEBIT PURCHASE CARD 1184                   14                blank    
          7-ELEVEN SYDNEY                                                      
          EFFECTIVE DATE 16 JUN 2020                                                                                                                                        
19 JUN    VISA DEBIT PURCHASE CARD 1184                 19.75               blank    
          DAISO SYDNEY                                                             
          DATE 17 JUN 2020  

and ends up in one line separating the DATE, DETAILS, FEE, FEE并以一行分隔 DATE、DETAILS、FEE、FEE

16 JUN, TRANSFER FROM ABC SOME COMPANY PTY LTD, BLANK, 150
18 JUN, VISA DEBIT PURCHASE CARD 1184, 7-ELEVEN SYDNEY, DATE 16 JUN 2020,14, blank
19 JUN, VISA DEBIT PURCHASE CARD 1184,DAISO SYDNEY, DATE 17 JUN 2020, 19.75, blank
                                                                             

This might work for you (GNU sed):这可能对你有用(GNU sed):

sed -E ':a;N;s/ {2,}|$/, /g
        /(,.*), (.*,.*, )\n(,.*)/{s//\1\3\2/;$!ba};s/, (\n|$)/\1/;P;D' file

Replace two or more spaces or the end of a line, by , , that is form columns.替换两个或多个空格或一个行的末尾,通过,即形式列。

Append the next line and if it begins with a comma, prepend it to the 3rd column and repeat.添加下一行,如果它以逗号开头,则将其添加到第 3 列并重复。

Otherwise, remove the introduced , at the end of the first line in the pattern space, print/delete the first line and repeat.否则,删除模式空间中第一行末尾的引入,打印/删除第一行并重复。

There is a great page: The Grymoire - AWK有一个很棒的页面: The Grymoire - AWK

If the data are in the data.txt file, give a try to this:如果数据在data.txt文件中,试试这个:

awk '
/^[0-9][0-9]*[[:blank:]]/ && NF > 3 {
  if (last_cols) {
    printf(", %s\n",last_cols);
  }
  printf("%s %s, ",$1, $2);
  last_cols=$(NF-1) " " $NR;
  for(i=3; i< NF-1; i++) {
    if (i>3) { 
      printf(" ");
    }
    printf("%s",$i);
  }
  next;
}
{ 
  if (NF>=1) {
    printf(", ");
  }
  for(i=1; i<= NF; i++) {
    printf(" %s",$i);
  }
}
END {
  printf(", %s\n",last_cols);
}' data.txt

generated output:生成的输出:

16 JUN,  TRANSFER,  FROM ABC SOME COMPANY PTY LTD, blank 16
18 JUN,  VISA DEBIT PURCHASE CARD 1184,  7-ELEVEN SYDNEY,  EFFECTIVE DATE 16 JUN 2020, 14 VISA
19 JUN,  VISA DEBIT PURCHASE CARD 1184,  DAISO SYDNEY,  DATE 17 JUN 2020, 19.75 CARD

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

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