使用awk将带有空格的第一列视为一列 [英] Treat first column with spaces as one column using awk

查看:374
本文介绍了使用awk将带有空格的第一列视为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有想要提取的数据.但是,由于第一列中有一些数据,因此我在第一列上遇到了麻烦,这使我很难使用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

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

运行会产生以下结果:

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

但是想要这个:

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/@/ /'

这会将第一个单词包含空格的行识别为具有额外的列(awk措辞中的字段),将空格打印为@符号,然后使用column -t进行格式设置,然后将@替换为空格使用sed.

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将带有空格的第一列视为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆