简体   繁体   English

使用 awk 将带空格的第一列视为一列

[英]Treat first column with spaces as one column using awk

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但是,我在第一列遇到问题,因为某些数据中有空间,这使我很难使用 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.尝试了我在另一个脚本中使用的这个命令,因为 output 有点相似,但它没有按预期工作。

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

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 .这会将第一个单词包含空格的行识别为具有额外列(awk 用语中的字段),将空格打印为@符号,然后使用column -t格式化,然后使用sed@替换为空格。

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

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