Awk按第一栏分组

时间:2018-05-23 02:32:17

标签: awk

我有这样的数据

prdnf001:app_prd_areas_sp2_ux  
prdnf001:app_prd_cx_local_ux  
prdnf001:app_prd_cx_ux  
prdnf001:app_prd_ftt_ux  
prdnf001:app_prd_local_ux 
prdnf001:app_prd_app_ux  
devnf001:app_syt_ds_backup1  
devnf001:app_syt_ds_sybsft1  
devnf001:app_syt_ftt_ux  
devnf001:app_syt_app_ux 
devnf001:app_uat_cx_ux   

我会输出分组喜欢这个:

prdnf001
prdnf001:app_prd_areas_sp2_ux  
prdnf001:app_prd_cx_local_ux  
prdnf001:app_prd_cx_ux  
prdnf001:app_prd_ftt_ux  
prdnf001:app_prd_local_ux 
prdnf001:app_prd_app_ux 
devnf001 
devnf001:app_syt_ds_backup1  
devnf001:app_syt_ds_sybsft1  
devnf001:app_syt_ftt_ux  
devnf001:app_syt_app_ux 
devnf001:app_uat_cx_ux   

到目前为止,这是我正在使用的代码。我究竟做错了什么? 感谢

awk 'split($1,svm,":");{if (svm[1] == f1) { print svm[1] }else print svm[1];f1=svm[1] }'

1 个答案:

答案 0 :(得分:0)

awk -F ':' 'saved!=$1{saved=$1; print saved} {print}' file

输出:

prdnf001
prdnf001:app_prd_areas_sp2_ux DP prdnf001-dr:app_prd_areas_sp2_ux_m Snapmirrored 
prdnf001:app_prd_cx_local_ux DP prdnf001-dr:app_prd_cx_local_ux_m Snapmirrored 
prdnf001:app_prd_cx_ux DP prdnf001-dr:app_prd_cx_ux_m Snapmirrored 
prdnf001:app_prd_ftt_ux DP prdnf001-dr:app_prd_ftt_ux_m Snapmirrored 
prdnf001:app_prd_local_ux DP prdnf001-dr:app_prd_local_ux_m Snapmirrored 
prdnf001:app_prd_app_ux DP prdnf001-dr:app_prd_app_ux_m Snapmirrored 
devnf001
devnf001:app_syt_ds_backup1 DP tstnf001:app_syt_ds_backup1_m Snapmirrored 
devnf001:app_syt_ds_sybsft1 DP tstnf001:app_syt_ds_sybsft1_m Snapmirrored 
devnf001:app_syt_ftt_ux DP tstnf001:app_syt_ftt_ux_m Snapmirrored 
devnf001:app_syt_app_ux DP tstnf001:app_syt_app_ux_m Snapmirrored 
devnf001:app_uat_cx_ux DP tstnf001:app_uat_cx_ux_m Snapmirrored