awk 行到多列

awk lines to multiple columns

用于对齐行的输入文本采用这种格式

(LINE_A) 是文件名,例如LINE_A 放在目录xy 该文件的内部是

file:G_VALUEFX:D_VALUEFX;SEAT01

正在返回

这个

7 LINE_A G_VALUEFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04 

(第 1 列中的数字返回行中的总列数)

我需要这行的帮助来从这行转换它们

例如

7 LINE_A G_VALUEFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04      
7 LINE_B G_VALUEFX D_VALUEFX SEAT22 SEAT25 SEAT27 SEAT30      
7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04      
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 SEAT30      

7 LINE_A    7 LINE_B    7 LINE_A     7 LINE_B 
G_VALUEFX   G_VALUEFX   G_VALUEFA    G_VALUEFA 
D_VALUEFX   D_VALUEFX   D_VALUEFA    D_VALUEFA 
SEAT01      SEAT22      SEAT01       SEAT22 
SEAT02      SEAT25      SEAT02       SEAT25 
SEAT03      SEAT27      SEAT03       SEAT27 
SEAT04      SEAT30      SEAT04       SEAT30 

(我不确定是否可以将其转换为这样的列对齐方式)

7 LINE_A   |  7 LINE_B   | 7 LINE_A   |  7 LINE_B 
G_VALUEFX  |  G_VALUEFX  | G_VALUEFA  |  G_VALUEFA 
D_VALUEFX  |  D_VALUEFX  | D_VALUEFA  |  D_VALUEFA 
SEAT01     |  SEAT22     | SEAT01     |  SEAT22 
SEAT02     |  SEAT25     | SEAT02     |  SEAT25 
SEAT03     |  SEAT27     | SEAT03     |  SEAT27 
SEAT04     |  SEAT30     | SEAT04     |  SEAT30 

有些行可能会更长或更短,例如

7 LINE_A G_VALUEFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04      
7 LINE_B G_VALUEFX D_VALUEFX SEAT22 SEAT25 SEAT27 SEAT30      
7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04      
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8    
7 LINE_C G_PREFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04      
8 LINE_G G_PREFX D_VALUEFX POSITION55 POSITION82 VALUE85 POSITION44 POSITION448
7 LINE_C G_PREFA D_VALUEFA SEAT01 SEAT02 SEAT03       
4 LINE_H G_PREFA D_VALUEFA SEAT22
5 LINE_H G_NAMEA D_EXPIRY5 SEAT01 SEAT02 
3 LINE_H G_NAMEA D_EXPIRY5 
7 LINE_B G_NAMEY D_EXPIRY1 SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8     

然后输出可能看起来像这样(给出的行数 = 更多列对齐/彼此放置)如果可能的话,使用列分隔符“|” 始终应该首先放置 LINE_A/B 第二个后跟 G 前缀的数字;第三个 D 前缀其余是具有随机信息的值 (为了方便起见在"LINE_A/B"之前不要加数字)

7 LINE_A    7 LINE_B     7 LINE_A      7 LINE_B                    7 LINE_C  8 LINE_G     7 LINE_C   4 LINE_H   5 LINE_H   3 LINE_H  7 LINE_B 
G_VALUEFX   G_VALUEFX    G_VALUEFA     G_VALUEFA                   G_PREFX   G_PREFX      G_PREFA    G_PREFA    G_NAMEA    G_NAMEA   G_NAMEY 
D_VALUEFX   D_VALUEFX    D_VALUEFA     D_VALUEFA                   D_VALUEFX D_VALUEFX    D_VALUEFA  D_VALUEFA  D_EXPIRY5  D_EXPIRY5 D_EXPIRY1
SEAT01      SEAT22       SEAT01        SEAT22                      SEAT01    POSITION55   SEAT01     SEAT22     SEAT01               SEAT22 
SEAT02      SEAT25       SEAT02        SEAT25                      SEAT02    POSITION82   SEAT02                SEAT02               SEAT25 
SEAT03      SEAT27       SEAT03        SEAT27                      SEAT03    VALUE85      SEAT03                                     SEAT27 
SEAT04      SEAT30       SEAT04        EXNUM899999SSSSS9S8S5S2S8   SEAT04    POSITION44                                              EXNUM899999SSSSS9S8S5S2S8 
                                                                             POSITION448  

谢谢

awk 救援!

$ awk -v OFS=' | ' '{a[NR,1]= FS ; 
                     for(j=3;j<=NF;j++) a[NR,j-1]=$j; 
                     maxNF=maxNF<NF?NF:maxNF}
                END {for(i=1;i<maxNF;i++)
                       for(j=1;j<=NR;j++)
                          printf "%s",a[j,i] (j==NR?ORS:OFS)}' file | column -ts'|'

7 LINE_A     7 LINE_B     7 LINE_A     7 LINE_B                     7 LINE_C     8 LINE_G       7 LINE_C     4 LINE_H     5 LINE_H     3 LINE_H     7 LINE_B
G_VALUEFX    G_VALUEFX    G_VALUEFA    G_VALUEFA                    G_PREFX      G_PREFX        G_PREFA      G_PREFA      G_NAMEA      G_NAMEA      G_NAMEY
D_VALUEFX    D_VALUEFX    D_VALUEFA    D_VALUEFA                    D_VALUEFX    D_VALUEFX      D_VALUEFA    D_VALUEFA    D_EXPIRY5    D_EXPIRY5    D_EXPIRY1
SEAT01       SEAT22       SEAT01       SEAT22                       SEAT01       POSITION55     SEAT01       SEAT22       SEAT01                    SEAT22
SEAT02       SEAT25       SEAT02       SEAT25                       SEAT02       POSITION82     SEAT02                    SEAT02                    SEAT25
SEAT03       SEAT27       SEAT03       SEAT27                       SEAT03       VALUE85        SEAT03                                              SEAT27
SEAT04       SEAT30       SEAT04       EXNUM899999SSSSS9S8S5S2S8    SEAT04       POSITION44                                                         EXNUM899999SSSSS9S8S5S2S8
                                                                                 POSITION448

如果删除管道 column 格式化程序,输出将用管道字符分隔(但不对齐)。