2016-12-15 87 views
0

sed/awk/perl格式化多個段落

dzinfo給我輸出的格式如下;

User: x0000001 
Forced into restricted environment: No 

    Role Name  Avail Restricted Env 
    --------------- ----- -------------- 
    login/Corp_All Yes None   
    _Example-Role-- Yes _Example-Role-- 
    ALL_Servers-Win  ALL_Servers-Win 
    /US_All    /US_All 
    Domain_GLOBAL-Ro Yes Domain_GLOBAL-Ro 
    le-CORE_Group-AL  le-CORE_Group-AL 
    L-MacOS/Domain_  L-MacOS/Domain_ 
    GLOBAL     GLOBAL  

    Effective rights: 
    Password login 
    Non password login 
    Allow normal shell 


PAM Application Avail Source Roles   
--------------- ----- -------------------- 
*    Yes login/US_All   
Privileged commands: 
    Name    Avail Command    Source Roles   
    --------------- ----- -------------------- -------------------- 
    CORP_GLOBAL-Com Yes /usr/bin/getfacl  CORP_GLOBAL-Role-COR 
    mand-CORE_SVR_I        E_SVR_INFRA_ALL-LNX/ 
    NFRA_ALL-V042-S        CORP_GLOBAL   
    00042/CORP_GLOB             
    AL                 
    CORP_GLOBAL-Com Yes /usr/bin/dzdo -l  CORP_GLOBAL-Role-COR 
    mand-CORE_SVR_I        E_SVR_INFRA_ALL-LNX/ 
    NFRA_ALL-V042-S        CORP_GLOBAL   
    00048/CORP_GLOB             
    AL                 
    CORP_GLOBAL-Com Yes /bin/cp temp_auth  CORP_GLOBAL-Role-COR 
    mand-CORE_SVR_I  /home/sudocfg/author E_SVR_INFRA_ALL-LNX/ 
    NFRA_ALL-V042-S  ized_keys    CORP_GLOBAL   
    00085/CORP_GLOB             
    AL                 

什麼工具將是格式化這樣的報告的最佳選擇?我怎麼能匹配/組合和格式的列/線如下所示?

User: x0000001 
Forced into restricted environment: No 

    Role Name            Avail Restricted Env 
    ---------------          ----- -------------- 
    login/Corp_All          Yes  None   
    _Example-Role--ALL_Servers-Win/US_All     Yes  _Example-Role--ALL_Servers-Win/US_All 
    Domain_GLOBAL-Role-CORE_Group-ALL-MacOS/Domain_GLOBAL Yes  Domain_GLOBAL-Role-CORE_Group-ALL-MacOS/Domain_GLOBAL 

    Effective rights: 
    Password login 
    Non password login 
    Allow normal shell 

    PAM Application Avail Source Roles   
    --------------- ----- -------------------- 
    *    Yes login/US_All   

Privileged commands: 
    Name                Avail Command            Source Roles   
    ---------------             ----- --------------------        -------------------- 
    CORP_GLOBAL-Command-CORE_SVR_INFRA_ALL-V042-S00042/CORP_GLOBAL Yes  /usr/bin/getfacl         CORP_GLOBAL-Role-CORE_SVR_INFRA_ALL-LNX/CORP_GLOBAL 
    CORP_GLOBAL-Command-CORE_SVR_INFRA_ALL-V042-S00048/CORP_GLOBAL Yes  /usr/bin/dzdo -l         CORP_GLOBAL-Role-CORE_SVR_INFRA_ALL-LNX/CORP_GLOBAL 
    CORP_GLOBAL-Command-CORE_SVR_INFRA_ALL-V042-S00085/CORP_GLOBAL Yes  /bin/cp temp_auth /home/sudocfg/authorized_keys  CORP_GLOBAL-Role-CORE_SVR_INFRA_ALL-LNX/CORP_GLOBAL 

在每列中的文本可以極大地變化,所以我想有寬度自動調整。

我可以處理單行,但對於這樣的報告?我不知道從哪裏開始。

+1

您可能想要尋求一個較小例子的幫助,然後您可以開發並增強自己以應用您的更大問題。沒有多少人願意通讀所有這些文字,試圖找出需要尋找的模式以及如何處理它們。 –

+1

即使有人可能會考慮回答這個問題,但沒有解決這個問題,但這對他們來說太大了。 :( – sjsam

+0

歡迎來到本站!請參閱[如何提問](https://stackoverflow.com/help/how-to-ask)瞭解更多關於讀者可能在尋找的問題。個人而言,我會使用' awk'因爲我對它很熟悉,看起來你想打開從'Avail' ='Yes'開始的行塊的列,你可以找到'Avail'和'Yes',開始累積,隨時打印進入下一個'是',然後在awk中看到空行或文件結尾時輸出最後一個塊('END {}'。) – cxw

回答

0

爲了讓您一開始,這裏基本上是你如何開始使用隔離GNU AWK的FIELDWIDTHS每條線路上的各個字段:

$ cat tst.awk 
BEGIN { origFS=FS } 
/---/ { 
    origFS=FS 
    split($0,f,/\s+|-+/,s) 
    FIELDWIDTHS="" 
    for (i=1; i in s; i++) { 
     FIELDWIDTHS = (i>1 ? FIELDWIDTHS " " : "") length(s[i]) 
    } 
} 
/^\s*$/ { 
    FIELDWIDTHS="" 
    FS=origFS 
} 
{ 
    for (i=1; i<=NF; i++) { 
     printf "<%s>", $i, (i<NF?OFS:ORS) 
    } 
    print "" 
} 

$ awk -f tst.awk file 
<User:><x0000001> 
<Forced><into><restricted><environment:><No> 

<Role><Name><Avail><Restricted><Env> 
<---------------><-----><--------------> 
< ><login/Corp_All >< ><Yes >< ><None   >< > 
< ><_Example-Role-->< ><Yes >< ><_Example-Role-><-> 
< ><ALL_Servers-Win>< ><  >< ><ALL_Servers-Wi><n> 
< ></US_All  >< ><  >< ></US_All ><> 
< ><Domain_GLOBAL-R><o ><Yes >< ><Domain_GLOBAL-><R> 
< ><le-CORE_Group-A><L ><  >< ><le-CORE_Group-><A> 
< ><L-MacOS/Domain_>< ><  >< ><L-MacOS/Domain><_> 
< ><GLOBAL   >< ><  >< ><GLOBAL  ><> 

<Effective><rights:> 
<Password><login> 
<Non><password><login> 
<Allow><normal><shell> 


<PAM><Application><Avail><Source><Roles> 
<---------------><-----><--------------------> 
<*    >< ><Yes >< ><login/US_All  >< > 
<Privileged comm><an><ds:><><><> 
< Name   >< >< Ava><i><l Command   >< > 
< -------------><-->< ---><-><- ------------------><-> 
< ><CORP_GLOBAL-Com>< ><Yes >< ></usr/bin/getfacl >< ><CORP_GLOBAL-Role-COR>< > 
< ><mand-CORE_SVR_I>< ><  >< ><     >< ><E_SVR_INFRA_ALL-LNX/>< > 
< ><NFRA_ALL-V042-S>< ><  >< ><     >< ><CORP_GLOBAL   >< > 
< ><00042/CORP_GLOB>< ><  >< ><     >< ><     >< > 
< ><AL    >< ><  >< ><     >< ><     >< > 
< ><CORP_GLOBAL-Com>< ><Yes >< ></usr/bin/dzdo -l >< ><CORP_GLOBAL-Role-COR>< > 
< ><mand-CORE_SVR_I>< ><  >< ><     >< ><E_SVR_INFRA_ALL-LNX/>< > 
< ><NFRA_ALL-V042-S>< ><  >< ><     >< ><CORP_GLOBAL   >< > 
< ><00048/CORP_GLOB>< ><  >< ><     >< ><     >< > 
< ><AL    >< ><  >< ><     >< ><     >< > 
< ><CORP_GLOBAL-Com>< ><Yes >< ></bin/cp temp_auth >< ><CORP_GLOBAL-Role-COR>< > 
< ><mand-CORE_SVR_I>< ><  >< ></home/sudocfg/author>< ><E_SVR_INFRA_ALL-LNX/>< > 
< ><NFRA_ALL-V042-S>< ><  >< ><ized_keys   >< ><CORP_GLOBAL   >< > 
< ><00085/CORP_GLOB>< ><  >< ><     >< ><     >< > 
< ><AL    >< ><  >< ><    ><><><> 

你會想要買的書有效AWK編程,第4版,由阿諾德·羅賓斯和有它方便的參考,當你開始與擺弄,看看它是如何工作的,然後就可以建立。

+1

爲什麼一個反對?問題是關於使用awk的解釋的答覆,套房完美地滿足了這種需求 – NeronLeVelu