2017-09-07 36 views
-2

我有一個包含id命令的詳細信息的文件test.txt(如:id sudi如何搜索特定羣體用戶屬於

uid=23351(sudi) gid=5000(bns) groups=5000(bns),16382(damepgrp),15657(edl_prd_rcrr_sup),15650(edl_prd_rcrr_user),15712(edl_prd_yarn_int_cb),15721(edl_prd_yarn_int_rsk),15632(edl_prod_rcam_admin),16110(edlp_bc5m_sup) 

uid=23458(achung1) gid=5000(bns) groups=5000(bns),15757(edl_prd_ice_analyst),15483(edl_prd_ice_user),15650(edl_prd_rcrr_user),15712(edl_prd_yarn_int_cb),15721(edl_prd_yarn_int_rsk) 

uid=19831(afabia) gid=5000(bns) groups=5000(bns),15774(edl_prd_c360da_users),15714(edl_prd_yarn_int_ana),15712(edl_prd_yarn_int_cb),15606(edl_prod_c360_users),16179(edl_prod_c360rt_users),15621(edl_prod_dspm_dev),16076(edlp_bc4g_ca_ez_usr),16052(edlp_bc4g_ca_usr),16053(edlp_c360_da_usr),15105(hdpcmo),15104(hdpnanalytics),15279(hdpnaug),15144(rscmo),15280(rsnaug),16171(edl_prod_c360rt_susr) 

如何打印唯一的ID和絲羣作爲輸出。我已經使用grep嘗試過和sed,但沒有運氣 如:

sudi: edl_prd_yarn_int_ana, edl_prd_yarn_int_cb 
achung1: edl_prd_yarn_int_cb, edl_prd_yarn_int_rsk 
afabia: edl_prd_yarn_int_cb, edl_prd_yarn_int_ana 
+1

將紗線信息總是被包裹在雙星號? – JNevill

+0

沒有我剛纔強調了id命令的問題目的輸出如下:UID = 23351(abotha)GID = 5000(BNS)基團= 5000(BNS),16382(damepgrp),15657(edl_prd_rcrr_sup),15650(edl_prd_rcrr_user ),15712(edl_prd_yarn_int_cb),15721(edl_prd_yarn_int_rsk),15632(edl_prod_rcam_admin),16110(edlp_bc5m_sup) –

回答

0

使用

gawk

$ awk -v RS='[()]' -v OFS=, '/yarn/{s=s?s OFS $1:$1}END{print "sudi: " s}' infile 
sudi: edl_prd_yarn_int_ana,edl_prd_yarn_int_cb 

awk

$ awk -F'[()]' -v OFS=, '{for(i=1; i<=NF; i++)if($i~/yarn/)s=s?s OFS $i:$i}END{print "sudi: " s}' infile 

輸入:

$ cat infile 
uid=21015(sudi) gid=5000(bns) groups=5000(bns),15714(edl_prd_yarn_int_ana),15712(edl_prd_yarn_int_cb),16083(edlp_bc4g_sup),16186(edlp_bc55_ah_da_usr1),16183(edlp_bc55_ah_da_usr2),16134(edlp_bc55_sup),16045(edlp_ds_na_usrgrp),15970(edlp_dsci_usr),15861(edlp_dsna_app),15956(edlp_pricinggtb_susr),15105(hdpcmo),15108(hdpcoe),15104(hdpnanalytics),15144(rscmo),15143(rsnanalytics),15152(rsnanalyticssec),15159(s2hdp19grp) 
0

grep -oP "uid=\d+\(\K\w+(?=\))|\w+yarn\w+" file | awk '$0~/yarn/{if(!first){a=a" "$1; first=1}else{a=a" ,"$0}} $0!~/yarn/{print a; a=$0":"; first=0} END{print a}'

輸出:

sudi: edl_prd_yarn_int_cb,edl_prd_yarn_int_rsk 
achung1: edl_prd_yarn_int_cb,edl_prd_yarn_int_rsk 
afabia: edl_prd_yarn_int_ana,edl_prd_yarn_int_cb 
+0

是的,那是一個,如果我有3個或更多的紗線用於紗線 –

+0

如果每個用戶有3個紗線用於將paste命令更改爲'paste - - - -'。但如果用戶有不同數量的紗字符串例如這是不行的。如果user1有3個紗線並且user2有4個紗線。在這種情況下這將失敗。你想要一個通用的解決方案嗎? – batMan

+0

我有2,3,4紗線字符串例如用戶:UID = 23391(XXXX)GID = 5000(BNS)基團= 5000(BNS),15714(edl_prd_yarn_int_ana),15712(edl_prd_yarn_int_cb),15718(edl_prd_yarn_int_gbm),16110( edlp_bc5m_sup),15829(edlp_cbt_tsz_ro),15143(rsnanalytics),15152(rsnanalyticssec)此用戶具有3紗線strings..so我需要一個通用的一個,其中,不論紗線串的數的它應該工作 –