Treat first column with spaces as one column using awk

∥☆過路亽.° 提交于 2020-06-02 09:33:26

问题


I have this data that wanted to extract. However, im having trouble with the first column since some data has space in it making it hard for me to parse it using awk

6_MB06 SA003              1550        None                                          uats           admin     1270           1478640      1211360         none    2957656064          no                               0                   no                                                                                                                            60021AA29H38028200000000000521D3   no         no               0                                  no              yes            no                      no                supported       no        no                                18                              2.60                      193                       no            0        Active optimized   0000         Not Available   
6_VLS01 G                 516         None                                          uats           admin     0              492880       176             none    1008291840          no                               0                   no                                                                                                                            60021AA29H38028200000000000521D4   no         no               0                                  no              yes            no                      no                supported       no        no                                99                              2.20                      0                         no            0        Active optimized   0000         Not Available   
6_VLS01 R                 1550        None                                          uats           admin     1361           1478640      1297994         none    2957656064          no                               0                   no                                                                                                                            60021AA29H38028200000000000521D5   no         no               0                                  no              yes            no                      no                supported       no        no                                12                              3.58                      375                       no            0        Active optimized   0000         Not Available   
irexenvdi_np_cl1_2_001    10956       None                                          ire_ct2_pool   admin     8689           10449056     8286854         none    21396484375         no                               0                   no                                                                                                                            60021AA29H38028200000000000521D6   no         no               0                                  no              yes            no                      no                supported       no        no                                20                              1.38                      1050                      no            0        Active optimized   0000         Not Available   
irexenvdi_np_cl1_2_002    10956       None                                          ire_ct2_pool   admin     8696           10449056     8293878         none    21396484375         no                               0                   no                                                                                                                            60021AA29H38028200000000000521D7   no         no               0                                  no              yes            no                      no                supported       no        no                                20                              1.36                      1132                      no            0        Active optimized   0000         Not Available   
4_MA04-SA025              1550        None                                          uats           admin     1270           1478640      1211856         none    2957656064          no                               0                   no                                                                                                                            60021AA29H38028200000000000630EC   no         no               0                                  no              yes            no                      no                supported       no        no                                18                              1.92                      316                       no            0        Active optimized   0000         Not Available   
4_G VLS01                 516         None                                          uats           admin     7              492880       7264            none    1008291840          no                               0                   no                                                                                                                            60021AA29H38028200000000000630ED   no         no               0                                  no              yes            no                      no                supported       no        no                                98                              1.26                      0                         no            0        Active optimized   0000         Not Available   
4_R VLS01                 1550        None                                          uats           admin     1278           1478640      1218864         none    2957656064          no                               0                   no                                                                                                                            60021AA29H38028200000000000630EE   no         no               0                                  no              yes            no                      no                supported       no        no                                17                              2.19                      423                       no            0        Active optimized   0000         Not Available   

Tried this command which i use in another script since output is somehow similar but it did not work as intended.

cat file | awk 'match($0, /[[:alnum:]]{32}/){ print $1, $2, substr($0, RSTART, RLENGTH)}' |column -t

Running that produces this:

6_MB06                  SA003  60021AA29H38028200000000000521D3
6_VLS01                 G      60021AA29H38028200000000000521D4
6_VLS01                 R      60021AA29H38028200000000000521D5
irexenvdi_np_cl1_2_001  10956  60021AA29H38028200000000000521D6
irexenvdi_np_cl1_2_002  10956  60021AA29H38028200000000000521D7
4_MA04-SA025            1550   60021AA29H38028200000000000630EC
4_G                     VLS01  60021AA29H38028200000000000630ED
4_R                     VLS01  60021AA29H38028200000000000630EE

But wanted this:

6_MB06 SA003              1550      60021AA29H38028200000000000521D3
6_VLS01 G                 516       60021AA29H38028200000000000521D4
6_VLS01 R                 1550      60021AA29H38028200000000000521D5
irexenvdi_np_cl1_2_001    10956     60021AA29H38028200000000000521D6
irexenvdi_np_cl1_2_002    10956     60021AA29H38028200000000000521D7
4_MA04-SA025              1550      60021AA29H38028200000000000630EC
4_G VLS01                 516       60021AA29H38028200000000000630ED
4_R VLS01                 1550      60021AA29H38028200000000000630EE

回答1:


If your first word has at most one space in it, you can try

awk 'NF == 35 { print $1"@"$2, $3, $15} NF == 34 { print $1, $2, $14 }' file | column -t | sed 's/@/ /'

This recognizes rows with the first word containing a space as having an extra column (field in awk parlance), printing the space as a @ symbol, then formatting with column -t, then replacing the @ with a space using sed.



来源:https://stackoverflow.com/questions/62075112/treat-first-column-with-spaces-as-one-column-using-awk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!